您好我正在制作这个接收POST请求的小型本地服务器,并返回所有大写的请求。它还会保存请求的本地副本以进行测试。我认为这行代码不是问题的根源,因为我尝试删除它并且问题仍然存在。问题是,当我尝试测试它时,它总是使用“错误:写完后”崩溃服务器。任何人都可以帮助为什么会这样吗?谢谢!
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);
这是完整的错误:
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)
答案 0 :(得分:1)
您将在完成之前结束响应。删除res.end()
。