当登录电子下面OnRequest功能烦恼node.js中调用的httpserver回调函数两次,一个页面刷新
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
日志将打印(在此啧啧http://www.nodebeginner.org/发现)“收到的请求。”每次刷新网页一次两次:这很烦人,因为这意味着在进行其他处理时可能会产生副作用。
为什么node.js不像其他http服务器那样缓解这种情况?
我的问题不是为什么我知道为什么,我的问题是如何检测到它是第二次,避免重复处理两次?
[Node.js可能重复 - 为什么我的回调被每次请求调用3次?](http://stackoverflow.com/questions/5369506/node-js-why-does-my-callback-get - 每次请求3次) –
尝试'console.log(request.url)',你会看到原因。 –
我的问题不是为什么我知道为什么,我的问题是如何检测到它是第二次,避免重复处理两次? – user310291