如果我在 Node 中编写以下程序:
http.createServer(function (req, res) {
if( req.method == 'GET' ) {
var body = ''; req.on('data', function(data) { body += data });
req.on('end', function() {
console.log('request ended')
});
}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('142\n');
}).listen(3500);
然后使用 http://xxx.xx.xxx.xx:35010
访问服务器不知道为什么单个 HTTP 请求会导致它执行两次。
最佳答案
这很正常 - 您的浏览器会调用多个电话。
例如,大多数浏览器都会调用抓取 /favicon.ico
。
尝试记录网址:
console.log(req.url);
你会看到被调用的内容。
关于node.js - nodejs - http.createServer 似乎调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11961902/