-
Question About Streaming Behavior in the Node.js ServerHi, I'm using the Node.js server script you provided at page 456 of the book, and I have a question regarding how responses are sent to the client. Observed BehaviorWhen I run the following curl -G http://localhost:8000/subsetSum \
--data-urlencode "data=[16,19,1,1,-16,9,1,-5,-2,17,-15,-97,19,-16,-4,-5,15]" \
--data-urlencode "sum=0" I expect the response to be streamed line by line as each match event is emitted. However, what I observe is that all the Code in QuestionThe server code contains the following logic: createServer((req, res) => {
const url = new URL(req.url, 'http://localhost');
if (url.pathname !== '/subsetSum') {
res.writeHead(200);
return res.end('I\'m alive!\n');
}
const data = JSON.parse(url.searchParams.get('data'));
const sum = JSON.parse(url.searchParams.get('sum'));
res.writeHead(200);
const subsetSum = new SubsetSum(sum, data);
subsetSum.on('match', match => {
res.write(`Match: ${JSON.stringify(match)}\n`);
});
subsetSum.on('end', () => res.end());
subsetSum.start();
}).listen(8000, () => console.log('Server started')); My Understanding & Questions
thanks for the help |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello @Ashilex, thanks for getting a copy of the book and for submitting this question. Buffering might happen for a variety of reasons (OS, Node.js version, etc) One way you can force the buffered data to be flushed on demand is to use the following pattern: writableStream.cork() // starts to buffer all following writes into memory
writableStream.write('...')
writableStream.uncork() // forces a flush of all the buffered data to the underlying writable resource So if we change the code as in the following screenshot (note lines 19 and 21), you should now see the expected behaviour: ![]() You can find more info about I hope this helps 😊 |
Beta Was this translation helpful? Give feedback.
Hello @Ashilex,
thanks for getting a copy of the book and for submitting this question.
Buffering might happen for a variety of reasons (OS, Node.js version, etc)
One way you can force the buffered data to be flushed on demand is to use the following pattern:
So if we change the code as in the following screenshot (note lines 19 and 21), you should now see the expected behaviour:
You can find more info about
writable.cork()
andwritable.uncork()
in the official Node.js docs.I hope thi…