您能帮我说一下这个简单的Web服务器有什么问题吗?
为什么validateWebhook
是undefined
之后const result = validateWebhook(req.body);
的声明和回调对象? 以及如何解决此问题?
看起来有点不对劲,我想念一些东西,但是不明白是怎么回事。
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
}
}
}
我要发送的HTTP请求来表达js:
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"
}
}
节点版本为 v10.5.0 。
答案 0 :(得分:3)
隐藏代码的内部部分以突出显示问题,这是您编写的内容:
const validateWebhook = ((filePath) => {
// Stuff, with no return statement
})('./schemas/waboxapp.json');
这是立即调用的函数表达式。它将立即调用(filepath) => {}
函数,传入'./schemas/waboxapp.json',然后将您返回的任何内容分配给validateWebhook。但是您什么也不返回,因此将其设置为undefined。