同步sequelize.sync()还是不同的方式?

问题描述 投票:0回答:1

我是新手,在基于Express的发电机项目上使用Sequelize。我正在使用Sequelize作为我的MySQL数据库,我需要在开始侦听来自网站的http请求之前启动数据库。首先我做的是:

app.js

sequelize.sync().then(() => {
    app.use('view engine', 'ejs');
    app.use('/auth', auth);
    //etc etc etc...
}).catch(err=>{
    console.log(err);
});

但有些人说我这不是正确的风格。还有更好的方法吗?或者在运行快速路线之前使用同步呼叫等,如下所示:

syncFunctionWhatCallsSequelizeSync();

app.use('view engine', 'ejs');
app.use('/auth', auth);
//etc etc etc...
node.js express asynchronous sequelize.js synchronous
1个回答
1
投票

您可以在sync()之前配置路由,中间件等,前提是您的服务器仅在sync()之后进行侦听。

// Configure routes
// Configure middleware
// Configure etc
models.sequelize.sync()
    .then(() => {
        // Start listening 
    })
    .catch(err => {

    });
© www.soinside.com 2019 - 2024. All rights reserved.