首页 > 解决方案 > 此节点js代码输出两个控制台日志,我不明白为什么

问题描述

这段代码有一个console.log输出两次的语句,我不明白为什么。有谁知道为什么会这样?

const http = require("http");
const fs = require("fs");
const _ = require("lodash");

const server = http.createServer((req, res) => {
  console.log("hey there"); //this outputs two times in the console for a request

  res.setHeader("Content-Type", "text/html");

  fs.readFile("./views/index.html", (err, data) => {
    if (err) {
      console.log("error");
      res.end();
    } else {
      res.end(data);
    }
  });
});

server.listen(3000, "localhost", () => {
  console.log("this is up and running in port 3000");
});

标签: node.jsoutputbackend

解决方案


当您使用浏览器发送请求时,向服务器发送了两个请求,其中一个请求与http://localhost:3000/favicon.ico照片差不多

在此处输入图像描述

最好发送一个请求postman


推荐阅读