An open API service indexing awesome lists of open source software.

https://github.com/envek/express-streaming-test

Application that demonstrates my trouble with streaming responses from Express.js app correctly..
https://github.com/envek/express-streaming-test

Last synced: 10 months ago
JSON representation

Application that demonstrates my trouble with streaming responses from Express.js app correctly..

Awesome Lists containing this project

README

          

# Express.js streaming responses and their testing

This application demonstrates my trouble with streaming responses from Express.js app correctly.

## The problem

1. **SOLVED** My end-to-end tests doesn't pass anymore (seems like they doesn't wait for all the data)

```js
expect((await request(app).get('/streaming')).body).toEqual({some: 'result'}); // But it gets {} instead of {"some":"result"}
```

Solution: set response type to `json` before streaming:

```js
response.type('json');
```

See [Faleij/json-stream-stringify#15 (comment)](https://github.com/Faleij/json-stream-stringify/issues/15#issuecomment-674257256) for details.

2. In logs wrong request runtime is being reported (request took 10 seconds but it says that it took 1 second, and no response body size info present, I suppose that it is time to first results being streamed):

```
without streaming:
GET /sync 200 14.797 ms - 43
with streaming (it executes for more than a second and writes the same 43 bytes):
GET /streaming 200 6.513 ms - -
```

Morgan package is used for logging (default generated by express-generator).

See discussion at https://gitter.im/expressjs/express?at=5f36c113a1190a2e95f1353e

## Setup

1. Install Node.js 12 (current LTS) or newer

2. Install packages

```sh
yarn install
```

## How to run

```sh
yarn start
```

and visit http://localhost:3000/

or

```sh
curl http://localhost:3000/sync
curl http://localhost:3000/streaming
```

## How to test

`yarn test` should pass but it doesn't.

## How to fix?

I don't know and need your help! Please open pull request if you know how to fix it!

## Where is the code?

- Application is in [app.js](./app.js)
- Tests are in [app.test.js](./app.test.js)