POST请求后执行模块

时间:2017-06-12 03:30:41

标签: javascript node.js express push-notification

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

如果我把所有内容放在同一个模块中,我不知道为什么在发送信息之后或发送信息之前不会执行发送通知的方法。

如果我运行命令npm start没有出现错误,但是一旦本地或远程服务器运行就会收到通知,这样我就不希望它发生了。

user.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;

notificacion.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);
        }); 

}; 

index.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;

3 个答案:

答案 0 :(得分:2)

使用以下代码替换您的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);

}; 

答案 1 :(得分:1)

在你的notification.js文件中,sendToAll函数将在需要文件时执行(这可能是你的运行时间。)

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

你想要将它包装在一个函数中并在你的帖子路径中调用它。

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

然后可以在服务器顶部使用

 const sendMessageFunction = require('path/to/notification.js')
 .
 .
 .
 sendMessageFunction('helloWorld')

答案 2 :(得分:0)

您是否尝试过将user.js编辑为:

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

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


/* 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(api);
    console.log(req.body);

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

module.exports = router;

和notification.js是这样的:

module.exports = function(api){ 

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

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

};