28 javascript spawn child-process node.js node-http-proxy
我是对应用程序执行spawn(子进程)的节点应用程序,应用程序有主机和端口:
var exec = require('child_process').spawn;
var child = exec('start app');
console.log("Child Proc ID " + child.pid)
child.stdout.on('data', function(data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function(data) {
console.log('stdout: ' + data);
});
child.on('close', function(code) {
console.log('closing code: ' + code);
});
Run Code Online (Sandbox Code Playgroud)
一些应用程序将立即启动,一些应用程序将需要一些时间10 - 20秒,直到它们启动.
现在我使用节点http代理来运行应用程序,问题是当用户想要在应用程序启动并运行之前运行应用程序时出现错误.知道我怎么能解决这个问题?
proxy.on('error', function (err, req, res) {
res.end('Cannot run app');
});
Run Code Online (Sandbox Code Playgroud)
顺便说一句,由于我们的框架限制,我无法在代理错误中发送响应500.任何其他想法我如何跟踪应用程序可能会有一些超时,以查看它发送响应的天气200.
更新 - 我的逻辑样本
httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
http.createServer(function (req, res) {
console.log("App proxy new port is: " + 5000)
res.end("Request received on " + 5000);
}).listen(5000);
function proxyRequest(req, res) {
var hostname = req.headers.host.split(":")[0];
proxy.web(req, res, {
target: 'http://' + hostname + ':' + 5000
});
proxy.on('error', function (err, req, res) {
res.end('Cannot run app');
});
}
Run Code Online (Sandbox Code Playgroud)
您需要的是侦听代理上的第一个响应并查看其状态代码以确定您的应用程序是否成功启动。操作方法如下:
proxy.on('proxyRes', function (proxyRes, req, res) {
// Your target app is definitely up and running now because it just sent a response;
// Use the status code now to determine whether the app started successfully or not
var status = res.statusCode;
});
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
895 次 |
| 最近记录: |