gpt4 book ai didi

javascript - node.js - 想在同一个路由器 POST 中发送两个资源

转载 作者:可可西里 更新时间:2023-11-01 09:26:04 26 4
gpt4 key购买 nike

我的路由器设置如下:

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 中。但是,我收到错误消息:发送后无法设置 header 。我很确定这是因为 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.

关于javascript - node.js - 想在同一个路由器 POST 中发送两个资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39333334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com