你能帮我看看这个简单的网络服务器有什么问题吗?
为什么 validateWebhook
在声明之后以及在此 const result = validateWebhook(req.body);
行的回调中立即变为 undefined
? 如何解决这个问题?
看起来有些不对劲,我错过了一些东西,但无法理解出了什么问题。
const express = require('express');
const fs = require('fs');
const Ajv = require('ajv');
const port = process.env.PORT || 3000;
const app = express();
/* Create validateWebHook function */
/* This function validates input JSON supplied to webhook */
const validateWebhook = ((filePath) => {
fs.promises.readFile(filePath, {options: 'utf8'})
.then((data) => {
console.log(`Validation schema [${filePath}]:`);
console.log(`${data}`);
const ajv = new Ajv();
const result = ajv.compile(data);
console.log(`Type is ${typeof result}`);
return result;
})
.catch((error) => {
console.log(`Error loading json schema [${filePath}]`);
console.log(`Details: [${error}]`);
});
})('./schemas/waboxapp.json');
console.log(`Type is ${typeof validateWebhook}`);
app.post('/', (req, res) => {
console.log(`Input JSON: ${req.body}`);
console.log(`Type is ${typeof validateWebhook}`);
const result = validateWebhook(req.body);
console.log(`Validation result: ${result}`);
res.sendStatus(200);
});
app.listen(port, () => {
console.log(`Server is up on port ${port}`);
});
这是 JSON 架构:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://lookin.im/schemas/input/waboxapp.json",
"type": "object",
"properties": {
"event": { "type": "string" },
"token": { "type": "string" },
"uid": { "type": "string" },
"contact": { "$ref": "#/definitions/contact" },
"message": { "$ref": "#/definitions/message" }
},
"required": [ "event", "token", "uid", "contact", "message" ],
"additionalProperties": false,
"definitions": {
"contact": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://lookin.im/schemas/input/contact.json",
"type": "object",
"properties": {
"uid": { "type": "string" },
"name": { "type": "string" },
"type": { "type": "string" }
},
"required": [ "uid", "name", "type" ],
"additionalProperties": false
},
"message": {
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://lookin.im/schemas/input/message.json",
"type": "object",
"properties": {
"dtm": { "type": "string" },
"uid": { "type": "string" },
"cuid": { "type": "string" },
"dir": { "type": "string" },
"type": { "type": "string" },
"ack": { "type": "string" },
"body": {
"type": "object",
"properties": {
"text": { "type": "string" }
},
"required": [ "text" ],
"additionalProperties": false
}
},
"required": [ "dtm", "uid", "cuid", "dir", "type", "ack", "body" ],
"additionalProperties": false
}
}
}
我发送给express js的HTTP请求:
POST / HTTP/1.1
Host: localhost:3000
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: 5d96ffe2-8f5f-477c-bc82-cd53147208c1
event=message&token=a09c8f3&uid=1&contact%5Buid%5D=1&contact%5Bname%5D=Name&contact%5Btype%5D=user&message%5Bdtm%5D=1&message%5Buid%5D=1&message%5Bcuid%5D=&message%5Bdir%5D=i&message%5Btype%5D=chat&message%5Bbody%5D%5Btext%5D=Test&message%5Back%5D=3
如何使用curl实用程序发送此请求:
curl -X POST \
http://localhost:3000/ \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Postman-Token: c319bdbe-16a9-42ab-a96b-2262e0c1fd81' \
-d 'event=message&token=a09c8f3&uid=1&contact%5Buid%5D=1&contact%5Bname%5D=Name&contact%5Btype%5D=user&message%5Bdtm%5D=1&message%5Buid%5D=1&message%5Bcuid%5D=&message%5Bdir%5D=i&message%5Btype%5D=chat&message%5Bbody%5D%5Btext%5D=Test&message%5Back%5D=3'
注意:我正在使用最新版本的依赖项。
{
"name": "test",
"version": "0.0.1",
"description": "Test",
"main": "app.js",
"author": "",
"license": "ISC",
"dependencies": {
"ajv": "^6.5.2",
"express": "^4.16.3"
}
}
Node 版本为v10.5.0。
最佳答案
挖出代码的内部部分以突出显示问题,这是您编写的内容:
const validateWebhook = ((filePath) => {
// Stuff, with no return statement
})('./schemas/waboxapp.json');
这是一个立即调用的函数表达式。它将立即调用 (filepath) => {}
函数,传入“./schemas/waboxapp.json”,然后您返回的任何内容都将分配给 validateWebhook。但您没有返回任何内容,因此它被设置为未定义。
关于javascript - 为什么 Node.js 中的函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51639487/