javascript 捕获 webhook node.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27852631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Catch webhook node.js
提问by Clip
I'm trying to catch a PUT/webhook request that is being made by the Aftership API in node.js. A PUT request is made each time a push notification is needed to be made, I am using Parse to send the notifications but I need some of the data from the webhook.
我正在尝试捕获由 node.js 中的 Aftership API 发出的 PUT/webhook 请求。每次需要推送通知时都会发出 PUT 请求,我使用 Parse 发送通知,但我需要来自 webhook 的一些数据。
The header of the webhook looks like Content-Type: application/json
And contains this data:
webhook 的标题看起来像Content-Type: application/json
并且包含以下数据:
ts - UTC unix timestamp that the event occurred
event - the name of the event (for tracking update, the value will be 'tracking_update')
msg - details about the message for which the event occurred, in the following format.
ts - 事件发生的 UTC unix 时间戳
event - 事件的名称(对于跟踪更新,该值将为“tracking_update”)
msg - 有关发生事件的消息的详细信息,格式如下。
How would I go about getting the tracking number, slug and the value for token
in the custom fields dictionary in node or js?
我将如何token
在 node 或 js 中的自定义字段字典中获取跟踪号、slug 和值?
{
"event": "tracking_update",
"msg": {
"id": "53aa94fc55ece21582000004",
"tracking_number": "906587618687",
"title": "906587618687",
"origin_country_iso3": null,
"destination_country_iso3": null,
"shipment_package_count": 0,
"active": false,
"order_id": null,
"order_id_path": null,
"customer_name": null,
"source": "web",
"emails": [],
"custom_fields": {},
"tag": "Delivered",
"tracked_count": 1,
"expected_delivery": null,
"signed_by": "D Johnson",
"shipment_type": null,
"tracking_account_number": null,
"tracking_postal_code": "DA15BU",
"tracking_ship_date": null,
"created_at": "2014-06-25T09:23:08+00:00",
"updated_at": "2014-06-25T09:23:08+00:00",
"slug": "dx",
"unique_token": "xk7LesjIgg",
"checkpoints": [{
"country_name": null,
"country_iso3": null,
"state": null,
"city": null,
"zip": null,
"message": "Signed For by: D Johnson",
"coordinates": [],
"tag": "Delivered",
"created_at": "2014-06-25T09:23:11+00:00",
"checkpoint_time": "2014-05-02T16:24:38",
"slug": "dx"
}]
},
"ts": 1403688191
}
回答by Andi N. Dirgantara
It can be done with Express
framework, example:
可以用Express
框架来完成,例如:
var express = require('express'),
bodyParser = require('body-parser'),
app = express(),
port = 3000;
app.use(bodyParser.json());
app.post('/', function (req, res) {
var body = req.body;
var trackingNumber = body.msg.tracking_number;
var slug = body.msg.slug;
var token = body.msg.unique_token;
console.log(trackingNumber, slug, token);
res.json({
message: 'ok got it!'
});
});
var server = app.listen(port, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
});
Here is the GIT repository, just clone it and do npm install
and then npm start
. The server will run on port 3000
:D
这是GIT 存储库,只需克隆它npm install
,然后执行npm start
。服务器将在端口上运行3000
:D
Note: I saw in Aftership Webhook's documentation, it said they will request POST
HTTP method, not PUT
so I create an example of post
request. Just replace it with put
if you want it to catch put
request.
注意:我在 Aftership Webhook 的文档中看到,它说他们将请求POST
HTTP 方法,PUT
所以我创建了一个post
请求示例。put
如果您希望它捕获put
请求,只需将其替换为。
回答by dkatavic
For inspecting webhooks data, I would suggest to store every request in database and then query database. As each request is different, easiest way would be creating API in sails.js (Node.js framework with easy to use ORM).
为了检查 webhooks 数据,我建议将每个请求存储在数据库中,然后查询数据库。由于每个请求都不同,最简单的方法是在 Sails.js(具有易于使用的 ORM 的 Node.js 框架)中创建 API。
sudo npm install sails -g
sails new _project_name_
cd _project_name_
sails generate api Records
With last command, sails has generated controller and model to store your webhook data.
使用 last 命令,sails 已生成控制器和模型来存储您的 webhook 数据。
I suggest installing pm2 for running app. you can run it with
我建议安装 pm2 来运行应用程序。你可以运行它
pm2 start app.js
Next you should configure your webhook in Aftership for following url:
接下来,您应该在 Aftership 中为以下 url 配置您的 webhook:
YOUR_SERVER_IP:PORT/Records/create
YOUR_SERVER_IP:端口/记录/创建
you can inspect data by following url:
您可以通过以下网址检查数据:
YOUR_SERVER_IP:PORT/Records/find
YOUR_SERVER_IP:端口/记录/查找
if you want to parse data, it can be done in RecordsController.js, for example:
如果要解析数据,可以在RecordsController.js中完成,例如:
Parsing: function(req, res) {
Records.find({}).exec(function(err, results) {
var output = [];
while (results.length) {
var result = results.pop();
//custom parsing goes here
//example:
output.push({
tracking_number: result.msg.tracking_number,
slug: result.msg.slug,
unique_token: result.msg.unique_token
});
}
return res.json(output);
});
},
You can call this method via following url: YOUR_SERVER_IP:PORT/Records/Parsing
您可以通过以下网址调用此方法:YOUR_SERVER_IP:PORT/Records/Parsing
I have created project in Codeanywhere for demonstration
我在 Codeanywhere 中创建了项目进行演示
webhook endpoint is: http://port-1337.zavtt4t8a0jm7vigncyo3txxmuhxgvix3yxk66pvydgqfr.box.codeanywhere.com/records/create
webhook 端点是:http: //port-1337.zavtt4t8a0jm7vigncyo3txxmuhxgvix3yxk66pvydgqfr.box.codeanywhere.com/records/create
For inspecting data, just replace /create part of url to /find
要检查数据,只需将 url 的 /create 部分替换为 /find
git repo is here: https://github.com/dkatavic/webhook_for_aftership
git repo 在这里:https: //github.com/dkatavic/webhook_for_aftership
you can just clone the project on your server and run it (or use my server for testing)
您可以在您的服务器上克隆项目并运行它(或使用我的服务器进行测试)
回答by vodolaz095
You can catch PUT request by
您可以通过以下方式捕获 PUT 请求
app.put('/someRouteToCatchWebHook', function(request, response) {
//webhook parsing goes here
});
(i'm sure that you use expressjs in your code - see http://expressjs.com/api.html#app.METHODfor details).
(我确定您在代码中使用了 expressjs -有关详细信息,请参阅http://expressjs.com/api.html#app.METHOD)。
If the webhook data is in request body, you can use the https://www.npmjs.com/package/body-parsermodule for parsing it.
如果 webhook 数据在请求正文中,您可以使用https://www.npmjs.com/package/body-parser模块对其进行解析。