const http = require('http')
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/plain')
console.log('Incoming')
res.write("Hello world, request granted!")
res.end()
})
server.listen(3000, 'localhost', () => {
console.log("Listening for requests on port 3000")
})
每当我启动服务器时,它都会显示“正在监听端口 3000 上的请求”,这是正确的。但是当我在 Chrome 上打开 localhost:3000 时,它会显示两次“传入”。为什么要两次? :D 我不明白。
最佳答案
默认情况下,所有浏览器都会尝试从每个站点获取 favicon.png
。这就是为什么您会看到两个请求发送到您的服务器。
如果您使用 postman 或curl 发送请求,您将只会看到一个请求。
提示:打开浏览器控制台上的网络选项卡,您将看到对网站图标的请求。
关于javascript - Node : Sending a request once, 但有两个输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68894774/