【问题标题】:when use req.end it prints two times in console. why?使用 req.end 时,它会在控制台中打印两次。为什么?
【发布时间】:2019-07-01 04:36:42
【问题描述】:

我是 Node.js 的新手

req.end 在 Node.js 中的用途是什么。我使用它,但是它打印了两次

app.use('/',function(req,res){
  res.send("hello world");  //send to browser
  console.log("by");  //print in console
  req.end;     //req is end here
}).listen(3000);

【问题讨论】:

  • req.end 或 res.end ?
  • 我只使用req.end
  • 您使用 Express 吗?没有像req.end这样的功能
  • 了解更多 res.send 和 res.end stackoverflow.com/questions/29555290/…
  • 更改为 res.end()。但它仍然可以在控制台中显示,如 G:\login & register>node demo i am the middle hello world i am the middle hello world

标签: node.js


【解决方案1】:
app.use('/',function(req,res){
  res.send("hello world");  //send to browser
  console.log("by");  //print in console
  req.end;     //req is end here
}).listen(3000);

在上面的代码中,当您从浏览器点击 localhost:3000/ 时,浏览器同时点击了两个请求,一个用于 favicon,一个用于 '/' 端点,并且您只创建 '/' 路由器,然后所有请求都会捕获'/' 路线。 当您从邮递员处点击此端点时,它将在控制台中打印一次。

【讨论】:

  • 解决了问题,了解了req,res
猜你喜欢
  • 2022-06-16
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多