抓住webhook node.js

Cli*_*lip 6 javascript put webhooks node.js

我正在尝试捕获由node.js中的Aftership API发出的PUT/webhook请求.每次需要推送通知时都会发出PUT请求,我正在使用Parse发送通知,但我需要来自webhook的一些数据.

webhook的标题看起来像Content-Type: application/json并包含这些数据:

ts - 事件发生的UTC unix时间戳

event - 事件的名称(对于跟踪更新,值将为'tracking_update')

msg - 有关事件发生的消息的详细信息,格式如下.

我如何获取token节点或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
}
Run Code Online (Sandbox Code Playgroud)

And*_*ara 4

可以用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)

});
Run Code Online (Sandbox Code Playgroud)

这是GIT 存储库,只需克隆它并执行npm install即可npm start。服务器将在端口3000:D上运行

注意:我在 Aftership Webhook 的文档中看到,它说他们将请求POSTHTTP 方法,PUT所以我创建了一个请求的示例postput如果您希望它捕获请求,只​​需将其替换为put