0

我的意图是在用户在“/”页面上执行第三方 API(例如 PayPal)之后;从第三方 API 系统接收到“/webhook/check”端点上的数据后,将数据中继到用户仍在等待响应的“/”索引页面。

下面是我的两个控制器。

index.js 控制器

router.get('/', function (req, res) {
    res.render('index', model);
});

webhooks.js 控制器

router.post('/check', function (req, res) {
    console.log(req.body);

    // WHEN A WEBHOOK NOTIFICATION IS RECEIVED HERE, DO SOMETHING
    // TO SEND DATA TO ALREADY LOADED `/' INDEX PAGE RENDERED BY
    // CONTROLLER ABOVE

    res.end();
});

更新:我使用了 socket.io

index.js

var io = require('socket.io').listen(server);

io.on('connection', function(socket){
    console.log('Socket, on connection, now sending/receiving on `webhooks`' );
    // socket.join("test");
    socket.on('webhooks', function(data){
        console.log(data);
    });


});

app.socketsio  = io;

webhooks.js

var socketsio = require('../index').socketsio;


module.exports = function (router) {

    var model = new WebhooksModel();

    router.post('/check', function (req, res) {

        // console.log(req.body);
        socketsio.emit('webhooks', req.body);
        res.end();
    });

};
4

1 回答 1

0

您可以使用 EventEmitter:

const EventEmitter = require('events');
const myEmitter = new EventEmitter();

router.get('/', function (req, res) {
    res.render('index', model);

    myEmitter.on('checkNotification', function(data) {
        // Process data
    })
});

router.post('/check', function (req, res) {
    console.log(req.body);

    myEmitter.emit('checkNotification', 'Your data')

    res.end();
});
于 2017-04-18T21:46:58.267 回答