简体   繁体   English

带有mysql pooling的Node.js http因错误意外退出

[英]Node.js http with mysql pooling quits unexpectedly on error

So I started to try node.js this morning and was able to come-up with a http service that handles path requests and can connect to mysql with pooling for multiple transactions. 因此,今天早上我开始尝试使用node.js,并能够提供一个处理路径请求的http服务,并可以通过池连接到mysql来处理多个事务。

I am just having problems if ever I tried to make the password wrong, etc the node process quits unexpectedly. 如果我尝试输入错误的密码,就会遇到问题,等节点进程意外退出。

var http = require("http");
var url = require("url");
var mysql = require('mysql');
var pool = mysql.createPool({
    host : 'localhost',
    user : 'root',
    password : 'root',
    database : 'test'
});

...

var pathname = url.parse(request.url).pathname;
var url_parts = url.parse(request.url, true);
var query = url_parts.query;

...

var table = query.table;
var sql = "SELECT * FROM " + table + "";

...

pool.getConnection(function(err, connection) {
        console.log(err);
        connection.on('error', function(err) {
            console.log(err.code);
        });

        // Use the connection
        connection.query(sql, function(err, rows) {
        if (err) throw err;
        console.log(rows);

        response.writeHead(200, {
            "Content-Type" : "application/json"
        });

        response.write(JSON.stringify(rows, null, 0));

        connection.end();
        response.end();
    });

    console.log(connection.sql);
    console.log(connection.query);
});

Appreciate any help on how can I make it not to QUIT! 感谢任何关于如何避免退出的帮助! and just say the damn error. 只是说该死的错误。

无论如何,在出现错误的情况下,我永远使该node.js永不退出。

您使用throw err ,但不要在任何地方捕获它,从而导致节点出现UncaughtException错误,从而关闭应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM