What is the best practice to connect/disconnect to a database?

前端 未结 4 1290
抹茶落季
抹茶落季 2021-02-09 23:37

I\'d like to know how to work with connectivity to a database in MEAN stack application. In particular, when should I create a connection to a database and when should I destroy

4条回答
  •  离开以前
    2021-02-10 00:27

    This is an opinion based question I'd say. What I use for my app is

    app.get('/', function (req, res) {
    res.sendfile('index.html');
    });
    mongoose.connect('mongodb://localhost:27017/my_db'); 
    

    This way I create a connection once rather than on every HTTP request. Your way should work fine but it seems you will have to connect and disconnect the db to your app way too many times specially when the app is in development.

提交回复
热议问题