Error when deploying DialogFlow CX webhook on Google Cloud Functions: "Error: listen EADDRINUSE: address already in use :::8080"
Error when deploying DialogFlow CX webhook on Google Cloud Functions: "Error: listen EADDRINUSE: address already in use :::8080"
我拼命尝试为我的 DialogFlow CX 代理实现一个简单的 Webhook。以前从未这样做过,所以我只是将我在下一页找到的 index.js 和 package.json 代码复制粘贴到我的 Google 云函数:DialogFlow CX calculate values
但这似乎不起作用。尝试部署 Cloud Function 时出现错误“错误:监听 EADDRINUSE:地址已在使用 ::8080”。
如果我使用这个示例代码,也会发生同样的情况:
我做错了什么?我正在编辑代码并尝试将其直接部署在 Google Cloude Web 控制台中,而不是通过命令提示符工具。
这里有更多细节:
Google 云函数的设置: 我通过 Google 云控制台设置了一个新的 Google 云函数,点击 创建函数。我将 Region 设置为 us-east1,Trigger type 设置为 HTTP 和 允许未经身份验证的调用 。然后我保存,更新 index.js 和 package.json 如下所述,然后单击 部署。结果是无法完成部署,因为 Error: listen EADDRINUSE: address already in use ::8080.
这里是我输入的代码 index.js:
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
var port = process.env.PORT || 8080;
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(bodyParser.json());
app.post('/BMI', (req, res) => processWebhook4(req, res));
var processWebhook4 = function(request, response ){
const params = request.body.sessionInfo.parameters;
var heightnumber = params["height.number"];
var weightnumber = params["weight.number"];
var heightunit = params["height.unit-height"]
var weightunit = params["weight.unit-weight"]
var computedBMI;
if (heightunit == "cm" && weightunit == "kg") { //using metric units
computedBMI = ((weightnumber/heightnumber/heightnumber )) * 10000;
} else if (heightunit == "in" && weightunit == "lb") { //using standard metrics
computedBMI = ((weightnumber/heightnumber/heightnumber )) * 703;
}
const replyBMI = {
'fulfillmentResponse': {
'messages': [
{
'text': {
'text': [
'This is a response from webhook! BMI is ' + computedBMI
]
}
}
]
}
}
response.send(replyBMI);
}
app.listen(port, function() {
console.log('Our app is running on http://localhost:' + port);
});
这里是我输入的代码 package.json:
{
"name": "cx-test-functions",
"version": "0.0.1",
"author": "Google Inc.",
"main": "index.js",
"engines": {
"node": "8.9.4"
},
"scripts": {
"start": "node index.js"
},
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2"
}
}
您分享的 Whosebug 帖子中的代码适用于其他平台,例如 Heroku。
您遇到的错误“Error: listen EADDRINUSE: address already in use :::8080”是因为代码函数监听了8080端口,请注意您需要检查并编辑您的示例代码提供,并查看所使用的库是否在 Google Cloud Functions 中受支持(例如:express js),以及这些库是否兼容以便在 Google Cloud Functions 中使用它。
这是来自此 Whosebug Post 的 Cloud Functions 的工作代码:
exports.helloWorld = (req, res) => {
const params = req.body.sessionInfo.parameters;
var heightnumber = params.height.number;
var weightnumber = params.weight.number;
var heightunit = params.height.heightUnit;
var weightunit = params.weight.weightUnit;
var computedBMI;
if (heightunit == "cm" && weightunit == "kg") { //using metric units
computedBMI = ((weightnumber/heightnumber/heightnumber )) * 10000;
} else if (heightunit == "in" && weightunit == "lb") { //using standard metrics
computedBMI = ((weightnumber/heightnumber/heightnumber )) * 703;
}
const replyBMI = {
'fulfillmentResponse': {
'messages': [
{
'text': {
'text': [
'This is a response from webhook! BMI is ' + computedBMI
]
}
}
]
}
}
res.status(200).send(replyBMI);
};
结果如下:
此外,这里还有一个示例代码,您也可以使用它在 Cloud Function 中进行部署:
index.js
exports.helloWorld = (req, res) => {
let fulfillmentResponse = {
"fulfillmentResponse": {
"messages": [{
"text": {
"text": [
"This is a sample response"
]
}
}]
}
};
res.status(200).send(fulfillmentResponse);
};
package.json
{
"name": "sample-http",
"version": "0.0.1"
}
部署示例代码后,您可以执行以下操作以使用 webhook:
- 转到管理 > Webhooks > 创建
- 添加显示名称和 Webhook URL(在 Cloud Functions 中触发 URL)
- 点击保存
- 转到“构建”>“流程”>“起始页”
- Select 任意路由并添加 webhook
- 在模拟器中测试
结果应该是这样的:
我拼命尝试为我的 DialogFlow CX 代理实现一个简单的 Webhook。以前从未这样做过,所以我只是将我在下一页找到的 index.js 和 package.json 代码复制粘贴到我的 Google 云函数:DialogFlow CX calculate values
但这似乎不起作用。尝试部署 Cloud Function 时出现错误“错误:监听 EADDRINUSE:地址已在使用 ::8080”。
如果我使用这个示例代码,也会发生同样的情况:
我做错了什么?我正在编辑代码并尝试将其直接部署在 Google Cloude Web 控制台中,而不是通过命令提示符工具。
这里有更多细节:
Google 云函数的设置: 我通过 Google 云控制台设置了一个新的 Google 云函数,点击 创建函数。我将 Region 设置为 us-east1,Trigger type 设置为 HTTP 和 允许未经身份验证的调用 。然后我保存,更新 index.js 和 package.json 如下所述,然后单击 部署。结果是无法完成部署,因为 Error: listen EADDRINUSE: address already in use ::8080.
这里是我输入的代码 index.js:
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
var port = process.env.PORT || 8080;
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(bodyParser.json());
app.post('/BMI', (req, res) => processWebhook4(req, res));
var processWebhook4 = function(request, response ){
const params = request.body.sessionInfo.parameters;
var heightnumber = params["height.number"];
var weightnumber = params["weight.number"];
var heightunit = params["height.unit-height"]
var weightunit = params["weight.unit-weight"]
var computedBMI;
if (heightunit == "cm" && weightunit == "kg") { //using metric units
computedBMI = ((weightnumber/heightnumber/heightnumber )) * 10000;
} else if (heightunit == "in" && weightunit == "lb") { //using standard metrics
computedBMI = ((weightnumber/heightnumber/heightnumber )) * 703;
}
const replyBMI = {
'fulfillmentResponse': {
'messages': [
{
'text': {
'text': [
'This is a response from webhook! BMI is ' + computedBMI
]
}
}
]
}
}
response.send(replyBMI);
}
app.listen(port, function() {
console.log('Our app is running on http://localhost:' + port);
});
这里是我输入的代码 package.json:
{
"name": "cx-test-functions",
"version": "0.0.1",
"author": "Google Inc.",
"main": "index.js",
"engines": {
"node": "8.9.4"
},
"scripts": {
"start": "node index.js"
},
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2"
}
}
您分享的 Whosebug 帖子中的代码适用于其他平台,例如 Heroku。
您遇到的错误“Error: listen EADDRINUSE: address already in use :::8080”是因为代码函数监听了8080端口,请注意您需要检查并编辑您的示例代码提供,并查看所使用的库是否在 Google Cloud Functions 中受支持(例如:express js),以及这些库是否兼容以便在 Google Cloud Functions 中使用它。
这是来自此 Whosebug Post 的 Cloud Functions 的工作代码:
exports.helloWorld = (req, res) => {
const params = req.body.sessionInfo.parameters;
var heightnumber = params.height.number;
var weightnumber = params.weight.number;
var heightunit = params.height.heightUnit;
var weightunit = params.weight.weightUnit;
var computedBMI;
if (heightunit == "cm" && weightunit == "kg") { //using metric units
computedBMI = ((weightnumber/heightnumber/heightnumber )) * 10000;
} else if (heightunit == "in" && weightunit == "lb") { //using standard metrics
computedBMI = ((weightnumber/heightnumber/heightnumber )) * 703;
}
const replyBMI = {
'fulfillmentResponse': {
'messages': [
{
'text': {
'text': [
'This is a response from webhook! BMI is ' + computedBMI
]
}
}
]
}
}
res.status(200).send(replyBMI);
};
结果如下:
此外,这里还有一个示例代码,您也可以使用它在 Cloud Function 中进行部署:
index.js
exports.helloWorld = (req, res) => {
let fulfillmentResponse = {
"fulfillmentResponse": {
"messages": [{
"text": {
"text": [
"This is a sample response"
]
}
}]
}
};
res.status(200).send(fulfillmentResponse);
};
package.json
{
"name": "sample-http",
"version": "0.0.1"
}
部署示例代码后,您可以执行以下操作以使用 webhook:
- 转到管理 > Webhooks > 创建
- 添加显示名称和 Webhook URL(在 Cloud Functions 中触发 URL)
- 点击保存
- 转到“构建”>“流程”>“起始页”
- Select 任意路由并添加 webhook
- 在模拟器中测试
结果应该是这样的: