javascript – node.js – 想在同一个路由器POST中发送两个res

我的路由器设置如下:

questionRouter.post('/questionsReply', (req, res) => {
 twilioResp(req, res);
 var newResponse = new Response(req.body);
 newResponse.save((err, data) => {
  if (err) return handleDBError(err, res);
  res.status(200).json(data);
 });
 console.log('From: ' + req.body.From);
 console.log('Message: ' + req.body.Body);
});

我试图将收到的回复保存到我的mongodb中.但是,我收到错误后发送无法设置标头.我很确定这是因为twilioResp()上面的函数.该代码是:

module.exports = exports = (req, res) => {
 var resp = new twilio.TwimlResponse();
 resp.message('Thank you! Your response "' + req.body.Body + '" has been saved!');
 res.writeHead(200, {'Content-Type': 'text/xml'});
 res.send(resp.toString());
};

此模块是对通过Twilio REST API接收的文本的自动响应.有没有办法在同一个POST请求中调用这两个?

解决方法:

Instead of res.send() you should use res.write() to send multiple responses.

res.send() sends entire HTTP response to the client includes headers and content even it ends the response.
And after that, you can’t send anything.

相关文章

文章浏览阅读552次。com.mongodb.MongoQueryException: Quer...
文章浏览阅读635次,点赞9次,收藏8次。MongoDB 是一种 NoSQ...
文章浏览阅读2.1k次。和。_mongodb 日期类型
文章浏览阅读1.7k次。Scalestack等客户期待使用MongoDB Atla...
文章浏览阅读970次。SpringBoot整合中间件mongodb、ES_sprin...
文章浏览阅读673次。MongoDB 简介_尚医通sql