javascript - 在 POST 请求后执行模块

标签 javascript node.js express push-notification

我正在尝试集成通过套接字发送实时信息(使用 socket.io),以及使用 OneSignal 平台发送推送通知。

碰巧如果我把所有东西都放在同一个模块里,不知道为什么发送通知的方法在发送信息之后没有执行,或者发送信息之前。

如果我运行命令 npm start 不会出现任何错误,但只要本地或远程服务器正在运行,通知就会到达,我不希望这种情况发生。

用户.js

  var express = require('express');
var router = express.Router();
var misocket = require('../routes/misocket');
var notificacion = require('../routes/notificacion');

/*

run module when start server run, i don't want it

notificacion();

*/ 

/* GET users listing. sendnote*/
router.post('/sendasig', function(req, res, next) { 


    console.log(misocket);//registrednote
    misocket.emit("registrar",req.body);  
  //run here, after the send data across post request
  notificacion();
    console.log(req.body);

  res.status(200).json({
      message  : "send message"
  }); 

});

module.exports = router;

通知.js

 module.exports = function(){ 

        var OnesignalNotificationApi = require('onesignal-notification');
        var api = new OnesignalNotificationApi('N2FkY2ZkZWQtMGQ2MS00ZTUwLTlkM2QtODA2NmE0YjBiMzQ3',
                                        'c4b92cce-4d59-4550-a4be-20939370e39c');

        var message = {
                it: 'Some message',
                en: 'Some message',
                es: 'Nueva Calificacion'
        };

        api.sendToAll(message, null, function(err, res){
                console.log(err);
                console.log(res);
        }); 

}; 

索引.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

misocket.js

var i = 0;
var ioapp;

exports.connection= function(io){

    ioapp = io;

    io.on('connect',function(s){
        console.log("Conectado");    
    });

};

exports.io = ioapp;

最佳答案

用下面的代码替换你的 notification.js

 module.exports = function(){ 

    var sendNotification = function (data) {
    var headers = {
        "Content-Type": "application/json; charset=utf-8",
        "Authorization": "Basic N2FkY2ZkZWQtMGQ2MS00ZTUwLTlkM2QtODA2NmE0YjBiMzQ3"
    };

    var options = {
        host: "onesignal.com",
        port: 443,
        path: "/api/v1/notifications",
        method: "POST",
        headers: headers
    };

    var https = require('https');
    var req = https.request(options, function (res) {
        res.on('data', function (data) {
            console.log("Response:");
            console.log(JSON.parse(data));
        });
    });

    req.on('error', function (e) {
        console.log("ERROR:");
        console.log(e);
    });

    req.write(JSON.stringify(data));
    req.end();
};

var message = {
    app_id: "c4b92cce-4d59-4550-a4be-20939370e39c",
    contents: {"en": "sample message"},
    included_segments: ["All"]
};

sendNotification(message);

}; 

关于javascript - 在 POST 请求后执行模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44491033/

相关文章:

javascript - 仅对于大数据,查找数组中最大数的总数会失败

javascript - Uncaught Error : define cannot be used indirect

node.js - 无法使用 ExpressJS 和 Zeit Now ('now dev' 访问静态公用文件夹)

javascript - TinyMCE/IE9/DIV 具有宽度/高度/内容可编辑/如何防止 DIV 选择?

javascript - 如何在 if...else if...else Javascript 语句中添加 CSS 代码

javascript - 渲染器中的 Electron 非上下文感知 native 模块

javascript - 在 Node.js 中导出 Mongoose 代码 - 将值从 Mongoose 方法传递给回调

node.js - 表达:req.params 与 req.body (JSON)

javascript - 根据父位置滚动 div

javascript - 如何使用 jQuery 添加第 n 个项目类