Wolfgaung
Wolfgaung

Reputation: 53

node app: setup routes after mongoose db setup?

I'm currently developing a web app. I just started the development and i try to setup the routes after the db (mongoose Schemas and connection) is ready because i need the models in my router.js file. My Code looks like this right now:

File: server.js

//configuration here...

mongoose.connect( "mongodb://" + config.database.host + "/" + config.database.dbName );
var db = mongoose.connection;
db.on( "error", console.error.bind(console, "connection error:" ));
db.once( "open", function () {
    var allModels = models.getAll( mongoose ); //retreive models from different files
    router.setupRoutes(express, app, allModels); //setup all the routes  also specified in other files
    console.log("connected to database and created the routes!");
});


app.listen(port, host, function() {
    console.log("");
    console.log("Server started at: " + "http://" + host + ":" + port);
    console.log("Press 'STRG+C' to stop the Server");
    console.log("");
});

My Question is: Is that the right way to do that? I call the app.listen function not inside this once("open") function (app.listen is at the bottom of this file and gets called before the routes are set up) My test routes are working but im not accesing the mongoose models right now. Should i call app.listen in the mongoose callback or like now at the end of my file?

i just want to make sure that this is the right way to do this. it seems to be the right way. mongoose said so... Look here

Upvotes: 0

Views: 1026

Answers (2)

Skandar B.A
Skandar B.A

Reputation: 51

I would like to point to specific case. when we put the connection in the server.js The app will try to connect when it is lunched. And If error has been occured it will throw the error and never look back. However, when we put the connection in the routes, the app will try to reach the db only when it needs to, and every time it needs to.The thing that make us able to reach db related end points and none related end points and open to db status changements. So If the db has a problem and we fix it, no need to restart the server.

Upvotes: 0

Adam Zerner
Adam Zerner

Reputation: 19258

I wouldn't say that there's a right or a wrong way. It depends what you're trying to do. Here are my thoughts:

Do you want to have your server running if the database connection isn't working? Maybe because some routes don't involve the database? If that's what you're aiming for, you'd have to set up the routes outside the db connection callback. Something like this:

db.once('open', function() {
  // set up routes that involve db connection
});

// set up routes that don't involve db connection
app.listen(port, host, function() {
});

If all of your routes involve database communication, I don't see a purpose to having app.listen outside of the db connection callback. Because the fact that the server is listening wouldn't be useful if the database connection is broken.

Upvotes: 1

Related Questions