【发布时间】:2021-12-15 16:34:29
【问题描述】:
使用 Node.js 设置新的 http 服务器
当你第一次启动它时,它会在屏幕上显示正确的 html 文本,但是当你移动到我的代码中的其他链接时,即:localhost:5001/about,它在我的 IDE 控制台中给我一个错误
events.js:377
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (internal/errors.js:322:7)
at writeAfterEnd (_http_outgoing.js:694:15)
at ServerResponse.end (_http_outgoing.js:815:7)
at Server.<anonymous> (/Users/angelo/Documents/coding/node/node-tutorial/app.js:11:7)
at Server.emit (events.js:400:28)
at parserOnIncoming (_http_server.js:900:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:127:17)
Emitted 'error' event on ServerResponse instance at:
at writeAfterEndNT (_http_outgoing.js:753:7)
at processTicksAndRejections (internal/process/task_queues.js:83:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
[nodemon] app crashed - waiting for file changes before starting...
app.js
const http = require('http');
const PORT = 5001;
const server = http.createServer((req, res) => {
if(req.url === '/'){
res.end('Home Page')
}
if(req.url === '/about'){
res.end('About Page')
}
res.end('Error page')
})
server.listen(PORT, () => {
console.log(`Server is listening on port: ${PORT}`)
})
我一直在寻找“结束后写入”错误的答案,但没有看到这个特定代码块的这个特定问题
【问题讨论】:
-
你的代码可以运行两次
res.end,当它出错时。
标签: javascript html node.js express