node.js - Are configuration files in Node&Express supposed to be read asynchronously? -


it naive question. have config file storing need making connection db. if reading asynchronously, connection done in callback?

fs.readfile(pathtoconfigfile, function (err, data){ createconnection(data);}); 

does ugly, suspicious , bit dangerous, doesn't it? (this example, i'd hear opinions it, if , why worng doing , on) in case using mongoose, query db on schema object (i.e. user.find()...) null results because connection doesn't happen synchronously guess.

thank much

(here app-structure)

app.js  db.js (read config file , make connection) config.json (info of db , others) user.js (model users) home.js (query users , list them on .get '/') 

it's ok sync operations during app start up. however, not sync stuff once app has started, while handling requests.

your app should configured start if database connectivity successful.

// set app var express = require('express') var app = express()  ....  // set database connectiveity var db = mongoose.connection; db.on('error', function() {   console.log('db connectivity error')   process.exit() }); db.once('open', function() {    // start app   app.listen()  }); 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -