简体   繁体   English

如何在Node.js中正确处理SequelizeConnectionError

[英]How to properly handle SequelizeConnectionError in nodejs

Below code check coming connection authenticated or not 下面的代码检查即将通过的连接是否已通过身份验证

    ..............
    ..............
        sequelize
            .authenticate()
            .then(() => {
                console.log('Connection has been established successfully.');
            })
            .catch(err => {

                console.error('Unable to connect to the database:', err);
            });
....................
.....................

above connection process only display the "error" in command prompt not response the user. 上面的连接过程仅在命令提示符下显示“错误”,而没有响应用户。 How to response the client(connection error) ? 如何响应客户端(连接错误)?

Not sure what is most popular or to best to learn, the expressjs website hello world example is about responding to a "/" path 'get' http request. 不确定最流行或最好学习的内容,expressjs网站您好,世界示例是关于响应“ /”路径“ get” http请求的。

In your web app example code and using express.js, you might be checking sequelize for a database connection on each request, in which case you would do something like: 在您的Web应用程序示例代码中,并使用express.js,您可能正在检查每个请求的数据库连接的续集,在这种情况下,您将执行以下操作:

app.get('/', function(res, req) {
    sequelize
        .authenticate()
        .then(() => {
            console.log('Connection has been established successfully.');
            //and do something like
            //send data to the client
        })
        .catch(err => {
            console.error('Unable to connect to the database:', err);
            //
            res.send('Unable to connect to the database.');
        });
});

This express.js documentation about basic routing might also be useful. 这个有关基本路由的 express.js 文档可能也很有用。

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

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