Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js express是否为每个请求提供不同的req和res对象?_Node.js_Express - Fatal编程技术网

Node.js express是否为每个请求提供不同的req和res对象?

Node.js express是否为每个请求提供不同的req和res对象?,node.js,express,Node.js,Express,我正在使用Express路由器,如下所示 router.post('/do', function(req, res, next) { // some sample code var questions = new Questions(); questions.display(req, res); }); display函数负责调用res.send 在上面的代码片段中,我创建了问题的实例,并将req,res对象传递给它 现在,从我的sample.js文件中,我向同一条路径发出

我正在使用
Express路由器
,如下所示

router.post('/do', function(req, res, next) {
   // some sample code
   var questions = new Questions();
   questions.display(req, res); 
});
display
函数负责调用
res.send

在上面的代码片段中,我创建了
问题
的实例,并将
req,res
对象传递给它

现在,从我的
sample.js
文件中,我向同一条路径发出了近5个ajax请求,只有一个请求成功,之后我得到了错误

Can't set headers after they are sent.
所以我不确定为什么会出现这个错误

有人能帮忙吗

更新

显示功能中的代码

Questions.prototype.display = function(req, res) {

  // Check if type of question exists
  if (req.body.questionType) {
    res.send({message: 'No Type found in request body!'});
  } else {
    // logic for getting questions from DB based on question type
    return res.send(questions);
  }
}
将请求更改为请求:

router.post('/do', function(req, res, next) {
// some sample code
var questions = new Questions(req, res);
questions.display(); 
}))


请参见

您的函数在将req传递给问题时接受请求。假设您打印的错误来自express服务器,则可以从您的问题和question.display()中查看其他代码。我正在以同样的方式使用express router运行一台服务器,在构建某些页面时,我向同一端点运行了10个请求,它运行得很好,所以这应该不是问题。@Robin:已用代码更新。我不确定您是否仍将req、res传递到问题中?变量问题=新问题();问题。显示(请求、回复)@里斯托诺维克:更新的问题。我正在将
req
res
传递到display方法中。这里缺少一些代码,您可以通过某种方式发送响应。另外,从post处理程序中删除next函数。您是否也在使用其他中间件?我的坏消息是,我尝试了不同的方法。当时它仍然是
request
,但现在再次更新。在res.send之前添加return({message:'在请求正文中找不到类型!'})if(req.body.questionType){return res.send({message:'在请求正文中找不到类型!');}else{//基于问题类型从DB获取问题的逻辑返回res send(questions);}