NodeJS – 如何匹配一次?

我有这个HTTP nodeJS服务器,但是当我访问它的url总是输出两次。

const https = require('http'); const fs = require('fs'); const url = require('url'); https.createServer((req, res) => { res.writeHead(200); try { var query =url.parse(req.url,true).query; if(query.id=='1') { console.log('OK matched'); } else { console.log('No match'); } res.end("req " + query.id); }catch(e) { console.log(e); } }).listen(8000); 

现在,当我打开http:// localhost:8000?id = donot-match,然后打印两次:

 No match No match 

当匹配发现它仍然打印

 OK matched No match 

如何确保行动只发生一次? (在没有匹配我必须执行一些exe不能执行两次,也是相同的匹配)

如果您在浏览器中触发此function,可能是浏览器正在请求图标,这与图标不匹配。