简体   繁体   中英

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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