POST请求后执行模块 [英] Execute module after POST request
问题描述
我正在尝试通过套接字(使用 socket.io )集成发送实时信息,并使用 OneSignal 平台发送推送通知。
I'm trying to integrate sending real-time information via sockets (using socket.io), and sending push notifications using the OneSignal platform.
如果我把所有内容放在同一个模块中,我不知道为什么发送信息后或发送信息之前不会执行发送通知的方法。
It happens that if I put everything in the same module, I do not know why the method to send the notification is not executed after the information is sent, or before sending the information.
如果我运行命令npm start没有出现错误,但是一旦本地或远程服务器运行,通知就会到达,这样我就不希望它发生了。
If I run the command npm start no error appears but the notification arrives as soon as the local or remote server is running, and this way I do not want it to happen.
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
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
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
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
Replace Your notification.js with the below code
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);
};
这篇关于POST请求后执行模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!