node.js中未处理的错误

时间:2017-04-04 05:56:10

标签: node.js mean-stack

app.get('/api/platforms2', function(req, res) {
    var sshcommectionresult = "came";
    c.connect({
        host: '10.254.27.9',
        port: 22,
        //debug: console.log,
        readyTimeout: 99999,
        username: 'bwadmin',
        password: 'bwadmin1'
    });

    c.on('connect', function() {
        console.log('Connection :: connect');
    });
    c.on('ready', function() {
        console.log('Connection :: ready');
        sshcommectionresult = "Connected";

        c.on('error', function(err) {

            console.log('Connection :: error :: ' + err);
            sys.debug("Unable to login");
            sshcommectionresult = err;
        });
        c.on('end', function() {
            console.log('Connection :: end');

        });
        c.on('close', function(had_error) {
            console.log('Connection :: close');
        });
        res.json(sshcommectionresult);
    });
});

//如果我有正确的用户名和密码,它就会连接。但是如果我提供了错误的用户名和密码。它显示未处理的错误。我无法解决此问题。

1 个答案:

答案 0 :(得分:0)

以下方法应该有效。

app.get('/api/platforms2', function(req, res) {
   var sshcommectionresult = "came";
   c.connect({
      //code
   });

   c.on('connect', function() {
     //code
   });

   c.on('ready', function() {
     //code
   });

  c.on('error', function(err){
    return res.json(err);
  });
});