Reputation: 359
Hello I am making this small local server that receives a POST request and returns the request with all upper cases. It also saves a local copy of the request for testing purposes. I don't think that line of code is the source of the problem, because I tried deleting it and the problem remains the same. The problem is that when I try to test it, it always crashes the server with an "error: write after end". Can anyone help why this is happening? Thanks!
var map = require('through2-map');
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, res) {
if (req.method !== 'POST') {
return res.end('Please send POST\n');
}
else {
req.pipe(fs.createWriteStream('post.txt'));
req.pipe(map(function (currValue) {
return currValue.toString().toUpperCase()
})).pipe(res);
res.end();
}
})
server.listen(8000);
This is the complete error:
events.js:160
throw er; // Unhandled 'error' event
^
Error: write after end
at ServerResponse.OutgoingMessage.write (_http_outgoing.js:441:15)
at Through2.ondata (C:\Users\ger\Documents\Javascript\nodeJS\node_modules\readable-stream\lib\_stream_readable.js:619:20)
at emitOne (events.js:96:13)
at Through2.emit (events.js:188:7)
at addChunk (C:\Users\ger\Documents\Javascript\nodeJS\node_modules\readable-stream\lib\_stream_readable.js:291:12)
at readableAddChunk (C:\Users\ger\Documents\Javascript\nodeJS\node_modules\readable-stream\lib\_stream_readable.js:278:11)
at Through2.Readable.push (C:\Users\ger\Documents\Javascript\nodeJS\node_modules\readable-stream\lib\_stream_readable.js:245:10)
at Through2.Transform.push (C:\Users\ger\Documents\Javascript\nodeJS\node_modules\readable-stream\lib\_stream_transform.js:148:32)
at Through2._transform (C:\Users\ger\Documents\Javascript\nodeJS\node_modules\through2-map\index.js:20:12)
at Through2.Transform._read (C:\Users\ger\Documents\Javascript\nodeJS\node_modules\readable-stream\lib\_stream_transform.js:184:10)
Upvotes: 0
Views: 9497
Reputation: 133008
You are ending the response before it has finished. Remove res.end()
.
Upvotes: 1