每个请求发送 Express.js 的多个响应
Sending multiple responses with Express.js per request
我正在学习节点 js,并且正在使用 Dialogflow 开发一个机器人。我想建立一个简单的机制:
- 第 1 步:dialogflow 发送带有 param1 参数的 POST 请求
- 第 2 步:我的应用程序通过 POST 响应以 JSON
的形式响应等待消息(示例:"your request is being processed")
- 第 3 步:我从 REST API
检索数据
- 第 4 步:使用 JSON
响应 DialogFlow 的 Webhood
我的问题(在第 4 步)是我无法为同一个请求发送两个响应并且我收到此错误消息:
2017-11-03T12:45:00.774506+00:00 app[web.1]: Error: Can't set headers after they are sent.
2017-11-03T12:45:00.774507+00:00 app[web.1]: at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
2017-11-03T12:45:00.774508+00:00 app[web.1]: at ServerResponse.header (/app/node_modules/express/lib/response.js:767:10)
2017-11-03T12:45:00.774508+00:00 app[web.1]: at ServerResponse.send (/app/node_modules/express/lib/response.js:170:12)
2017-11-03T12:45:00.774509+00:00 app[web.1]: at searching.then (/app/index.js:89:21)
2017-11-03T12:45:00.774509+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:109:7)
这是我的代码:
const express = require('express');
const bodyParser = require('body-parser');
//middleware
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/webhook', function (req, res, next) {
if (req.body.result && req.body.result.parameters && req.body.result.parameters.param1) {
// First response
var speechObject = {
speech: "your request is being processed",
displayText: "your request is being processed",
source: 'webhook-nodejs-api'}
var json_1 = JSON.stringify(speechObject)
res.send(json_1)
// Second response
searching_data_from_api().then(() => {
console.log("return second response JSON")
var dataObject = { type_pizza: "4 formages" }
var eventObject = { name: "pizza_est_prete", data: dataObject }
var json_2 = JSON.stringify({
followupEvent: eventObject
})
res.send(json_2);
return res.end()
}).catch(error => {
console.log(error)
})
} else {
var speech = "There is a problem. Try Again"
return res.json({
speech: speech,
displayText: speech,
source: 'webhook-nodejs-api'
})
console.log("There is a problem. Try Again")
}
})
app.listen((process.env.PORT || 8000), function () {
console.log("Server up and listening");
});
我的问题是,如何发送第二个答案?我尝试:
- res.write (JSON.stringify (data1))
- res.write (JSON.stringify (data2))
- res.end ()
但是没有用。感谢您的帮助!
你应该看看followup events
基本上,您需要立即响应,然后加载数据,然后 POST 将它们发送到您的机器人。
看到这个:https://discuss.api.ai/t/fulfilment-using-webhook-takes-time-to-get-the-data/3726/7
如果您使用的是 Dialogflow 的内置集成,则只能在对用户输入的直接响应中发出响应,因此不可能对单个请求响应两次。
如果您决定不使用内置集成,则可以编写一些代码将原始 "your request is being processed" 响应发送给用户,执行所需的处理,然后发送后续消息自己回应。您将执行以下操作:
- 处理来自用户使用的任何平台的传入请求
- 通过
/query
API 端点 将用户的查询发送到 Dialogflow
- 分析 Dialogflow 的响应以确定匹配的意图。如果它是一个需要调用延迟处理的意图,请调用它。
- 发送"your request is being processed"给用户。
- 延迟处理完成后,将响应发送给用户。
请务必注意,您将自己将最终响应发送给用户; Dialogflow 不会涉及。
我正在学习节点 js,并且正在使用 Dialogflow 开发一个机器人。我想建立一个简单的机制:
- 第 1 步:dialogflow 发送带有 param1 参数的 POST 请求
- 第 2 步:我的应用程序通过 POST 响应以 JSON 的形式响应等待消息(示例:"your request is being processed")
- 第 3 步:我从 REST API 检索数据
- 第 4 步:使用 JSON 响应 DialogFlow 的 Webhood
我的问题(在第 4 步)是我无法为同一个请求发送两个响应并且我收到此错误消息:
2017-11-03T12:45:00.774506+00:00 app[web.1]: Error: Can't set headers after they are sent.
2017-11-03T12:45:00.774507+00:00 app[web.1]: at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
2017-11-03T12:45:00.774508+00:00 app[web.1]: at ServerResponse.header (/app/node_modules/express/lib/response.js:767:10)
2017-11-03T12:45:00.774508+00:00 app[web.1]: at ServerResponse.send (/app/node_modules/express/lib/response.js:170:12)
2017-11-03T12:45:00.774509+00:00 app[web.1]: at searching.then (/app/index.js:89:21)
2017-11-03T12:45:00.774509+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:109:7)
这是我的代码:
const express = require('express');
const bodyParser = require('body-parser');
//middleware
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/webhook', function (req, res, next) {
if (req.body.result && req.body.result.parameters && req.body.result.parameters.param1) {
// First response
var speechObject = {
speech: "your request is being processed",
displayText: "your request is being processed",
source: 'webhook-nodejs-api'}
var json_1 = JSON.stringify(speechObject)
res.send(json_1)
// Second response
searching_data_from_api().then(() => {
console.log("return second response JSON")
var dataObject = { type_pizza: "4 formages" }
var eventObject = { name: "pizza_est_prete", data: dataObject }
var json_2 = JSON.stringify({
followupEvent: eventObject
})
res.send(json_2);
return res.end()
}).catch(error => {
console.log(error)
})
} else {
var speech = "There is a problem. Try Again"
return res.json({
speech: speech,
displayText: speech,
source: 'webhook-nodejs-api'
})
console.log("There is a problem. Try Again")
}
})
app.listen((process.env.PORT || 8000), function () {
console.log("Server up and listening");
});
我的问题是,如何发送第二个答案?我尝试: - res.write (JSON.stringify (data1)) - res.write (JSON.stringify (data2)) - res.end ()
但是没有用。感谢您的帮助!
你应该看看followup events
基本上,您需要立即响应,然后加载数据,然后 POST 将它们发送到您的机器人。
看到这个:https://discuss.api.ai/t/fulfilment-using-webhook-takes-time-to-get-the-data/3726/7
如果您使用的是 Dialogflow 的内置集成,则只能在对用户输入的直接响应中发出响应,因此不可能对单个请求响应两次。
如果您决定不使用内置集成,则可以编写一些代码将原始 "your request is being processed" 响应发送给用户,执行所需的处理,然后发送后续消息自己回应。您将执行以下操作:
- 处理来自用户使用的任何平台的传入请求
- 通过
/query
API 端点 将用户的查询发送到 Dialogflow
- 分析 Dialogflow 的响应以确定匹配的意图。如果它是一个需要调用延迟处理的意图,请调用它。
- 发送"your request is being processed"给用户。
- 延迟处理完成后,将响应发送给用户。
请务必注意,您将自己将最终响应发送给用户; Dialogflow 不会涉及。