Closed harishmahajan closed 6 years ago
Can't set headers after they are sent. at validateHeader (_http_outgoing.js:491:11)
This error is not coming from JSONStream
. It's happening because in the es.mapSync
callback you're writing data to the res
by using res.json({ ... })
. This happens multiple times if there are multiple items in the results from the request
call.
Check the express
documentation, but I'm pretty sure that res.json
will send headers back to the client whenever it's called. Instead, you can try piping the data back:
router.get('/', function(req, res, next) {
request({url: 'https://jsonplaceholder.typicode.com/posts/1'})
.pipe(JSONStream.parse('*'))
.pipe(es.mapSync(function (data) {
return { 'data': data, 'success': 'hello' };
}))
.pipe(JSONStream.stringify())
.pipe(res);
I am getting below error.
internal/streams/legacy.js:59 throw er; // Unhandled stream error in pipe. ^ Error: Can't set headers after they are sent. at validateHeader (_http_outgoing.js:491:11) at ServerResponse.setHeader (_http_outgoing.js:498:3) at ServerResponse.header (D:\Demos\leaflet-map\node_modules\express\lib\response.js:767:10) at ServerResponse.send (D:\Demos\leaflet-map\node_modules\express\lib\response.js:170:12) at ServerResponse.json (D:\Demos\leaflet-map\node_modules\express\lib\response.js:267:15) at D:\Demos\leaflet-map\routes\users.js:18:9 at Stream.write (D:\Demos\leaflet-map\node_modules\event-stream\index.js:204:20) at Stream.stream.write (D:\Demos\leaflet-map\node_modules\through\index.js:26:11) at Stream.ondata (internal/streams/legacy.js:16:26) at emitOne (events.js:116:13) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! leaflet-map@0.0.0 start:
node ./bin/www
npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the leaflet-map@0.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\hmahajan\AppData\Roaming\npm-cache_logs\2018-07-23T11_54_11_153Z-debug.log============================ users.js
==============================
what is the issue here??