Why my node server processes the request twice?

前端 未结 2 874
遇见更好的自我
遇见更好的自我 2021-01-25 12:35

I have following simple node server.

const http = require(\'http\');

http.createServer(function(req, resp) {

    console.log(\"request arrived.\")
    resp.wri         


        
2条回答
  •  面向向阳花
    2021-01-25 13:01

    In answer to the follow-up question "I have checked.But how we can avoid it?" answer is put the lines in previous answer just below the line that has http.createServer, so that you end up with something like following which contains 4 lines added to the original code in question:

    const http = require('http');
    
    http.createServer(function(req, resp) {
        if (req.url === '/favicon.ico') {
          res.end();
          return;
        }
    
        console.log("request arrived.")
        resp.writeHead(200, { 'Content-Type': 'application/json' });
        resp.end("Hello world!!");
    
    }).listen(3000);
    

提交回复
热议问题