I try to read stream request line by line and use split module for it. But I got an error when I try set header connection:close when data is big!
Example code:
const http = require('http');
const split = require('split');
server.on('request', (req, res) => {
let size = 0;
req
.pipe(split())
.on('data', chunk => {
size += chunk.length;
if (size > 1024) {
res.statusCode = 413;
res.setHeader('Connection', 'close');
res.end('File is too big!');
}
})
.on('end', () => {
res.end('OK');
});
}
Error: Can't set headers after they are sent.
How I can stop browser streaming without set a header and how correctly read line-by-line request stream in this case?