我正在使用node.js,在这里我看到此res.end(callback)不受控制。请帮助我获得相关的答案。
const http = require('http');
const body = ' Appending text';
const server = http.createServer((req,res)=> {
res.write("Hello , I created my first server ");
res.end(body,afterend)
});
server.listen(2000);
console.log("server is up and running at 2000");
function afterend(){
console.log("response ended")
}
预期的控制台输出: 服务器已启动并在2000年运行 回应结束 **
答案 0 :(得分:1)
那是正常的。实际上,不是两次被调用的回调。这是createServer方法。大多数浏览器都会调用/favicon.ico。带有req.url的console.log将向您显示正在发生的事情
const http = require('http');
const body = ' Appending text';
const server = http.createServer((req, res) => {
console.log('Who is getting called here', req.url);
res.write("Hello , I created my first server ");
res.end(body, afterend)
});
server.listen(2000);
console.log("server is up and running at 2000");
function afterend() {
console.log("response ended")
}