Error [ERR_STREAM_WRITE_AFTER_END]: write after end
代码如下
// 用于创建网站服务器的模块
const http = require('http');
// app 对象就是网站服务器对象
const app = http.createServer();
// 当客户端有请求来的时候
app.on('request', (req, res) => {
// 获取请求方式
// req.method
// console.log(req.method);
// 获取请求地址
// req.url
console.log(req.url);
if (req.url == '/index' || req.url == '/') {
res.end('Welcome to homepage');
} else if (req.url == '/list') {
res.end('Welcome to listpage');
} else {
res.end('not found');
}
if (req.method == 'POST') {
res.end('post');
} else if (req.method == 'GET') {
res.end('get');
}
res.end('<h2>hello user</h2>');
});
// 监听端口
app.listen(3000);
console.log('网站服务器启动成功');
问题
nodejs搭建简易http服务器的时候,命令窗口报错 Error [ERR_STREAM_WRITE_AFTER_END]: write after end
解决
根据网上的方法,试过将npm更新到最新版或者npm5.6.0版本,都不行
最后发现是 res.end()方法重复使用导致,将末尾的res.end()方法注释即可正常运行