[英]make node.js not exit after ssh2 connection error
I have the following code in my node.js app to test the SSH connection to a server using ssh2 : 我的node.js应用程序中包含以下代码,以使用ssh2测试与服务器的SSH连接:
var Connection = require('ssh2');
var conn = new Connection();
conn.on('ready', function() {
console.log('Connection :: ready');
conn.exec('uptime', function(err, stream) {
if (err) throw err;
stream.on('exit', function(code, signal) {
console.log('Stream :: exit :: code: ' + code + ', signal: ' + signal);
}).on('close', function() {
console.log('Stream :: close');
conn.end();
}).on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});
}).connect({
host: serverIp,
port: serverPort,
username: serverUsername,
privateKey: require('fs').readFileSync('./id_rsa')
});
It works fine, but if it returns an error, if I get a connection timeout for example, the node.js app crashes... 它工作正常,但是如果返回错误,例如,如果我得到连接超时,则node.js应用程序将崩溃...
I would like to do the following : 我想做以下事情:
I tried to change the "if (err) throw err;" 我试图更改“ if(err)throw err;”。 line with "if (err) res.redirect('/servers');"
符合“ if(err)res.redirect('/ servers');” but it doesn't work...
但这行不通...
Can someone help me with this noobie question ? 有人可以帮我解决这个问题吗? I'm new to node.js :)
我是node.js的新手:)
thanks a lot. 非常感谢。
Listen to the error
event: 听
error
事件:
conn.on('error', function(e) { /* do whatever */ });
Right now, your process crashes b/c that connection error is unhandled. 现在,您的进程因未解决连接错误而崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.