Javascript 如何从';next()';ExpressJS中的函数
我在node.js应用程序中使用服务器发送的事件。我尝试实现一个在linux机器上监视进程的服务 因此,我尝试获取流程的PID,并使用Javascript 如何从';next()';ExpressJS中的函数,javascript,node.js,express,server-sent-events,Javascript,Node.js,Express,Server Sent Events,我在node.js应用程序中使用服务器发送的事件。我尝试实现一个在linux机器上监视进程的服务 因此,我尝试获取流程的PID,并使用监控PID读取有关流程的一些信息 我想要实现的目标: 当我找到一个有效的PID时,我调用next()路由到打印出我的monitor PID对象内容的函数 此对象有一个end侦听器,在删除进程的PID时调用该侦听器。在这个end监听器中,我想“返回”到app.get(“/”) 我用app.redirect(“/”)尝试了它,但这不起作用,因为在第一个路由中已经设置了
监控PID
读取有关流程的一些信息
我想要实现的目标:
当我找到一个有效的PID时,我调用next()
路由到打印出我的monitor PID
对象内容的函数
此对象有一个end
侦听器,在删除进程的PID时调用该侦听器。在这个end
监听器中,我想“返回”到app.get(“/”)
我用app.redirect(“/”)尝试了它,但这不起作用,因为在第一个路由中已经设置了用于发送服务器发送事件的HTTP头
问题:
如何跳回调用next()
的函数?是否有类似于before()的
代码:
mp
对象是在getPidSync()函数中创建的
var express = require('express');
var app = express();
var MonitorPid = require('monitor-pid');
var mp;
app.get('/', function (req, res, next) {
var interval;
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive"
});
var pidTmp;
interval = setInterval(function() {
if(typeof pidTmp === "undefined" || pidTmp === "") {
pidTmp = getPidSync();
} else {
next();
}
}, 1000);
req.connection.addListener("close", function () {
if(mp != null) mp.stop();
if(typeof interval !== 'undefined') clearInterval(interval);
}, false);
}, function(req, res) {
if(typeof mp !== 'undefined') {
mp.on('monitored', function (pid, stats) {
res.write('data:' + JSON.stringify(stats) + '\n\n');
});
mp.on('end', function (pid) {
res.redirect('/');
console.error('end', pid);
});
}
});
app.listen(3002, function () {
console.log('Example app listening on port 3002');
});