AdiFatLady
AdiFatLady

Reputation: 368

Best way for Node JS to wait on start up for initialisation from database etc

I know Node is non blocking etc but i dont know how to solve this issue without blocking.

You start the server

node app.js

but you need some config etc from a database or mongodb before you deal with incoming requests, so you need to wait for the db response to return before you launch into dealing with taking requests.

I could use nimble but then you have to wrap the routes etc all in a second execution block which is nasty.

Whats the best way?

Upvotes: 1

Views: 5662

Answers (3)

AdiFatLady
AdiFatLady

Reputation: 368

Based on the significance of the server.listen role in the sequence i used nimble and did the following....

In the first block i get the stuff from the db (elastic search in this case) and do some manipulation of it that is required to build the routes in the second block, then in the last block i start the server. You could use nimble to do some other pre init tasks and just do a parallel block inside the first serial block too.

var chans = [];
flow.series([
    function (cb) {
        esClient.search({
            ...
        }).then(function (resp) {
            var channels = resp.hits.hits;

            channels.forEach(function(chan){chans.push(chan.urlSlug)});
            chans = chans.join('|');
            cb();
            });
    },
    function(cb) {
         app.get('/:type('+types+')/[a-z\-]+/[a-z0-9\-]+-:id/', itemRt.feature);//http://localhost:3000/how-to/apple/apple-tv-set-up-tips-3502783/
        app.get('/:urlSlug('+types+')/', listingRt.category);
        app.get('/:urlSlug('+chans+')/', listingRt.channel);
        cb();
    },
    function(cb) {
        server.listen(app.get('port'), function(){
            console.log('Express server listening on port ' + app.get('port'));
            cb();
        });
    }
]);

Upvotes: 0

clay
clay

Reputation: 6017

It is OK to block for things that are necessary. Don't go asynchronous just for the sake of it!

In this case, since the DB is crucial to your app even running, blocking until it is ready is appropriate (and probably saving you a lot of hassle of handling calls that don't have a DB connected yet).

You could also postpone starting your app server (in a callback, promise, etc) until the call to start the DB completes. Though since nothing else is happening until the app is initialized (from what I can tell in the question), it wouldn't matter either way because you're not stealing that single-thread from anything else!

Upvotes: 2

sgress454
sgress454

Reputation: 24958

Node is indeed non-blocking, but that doesn't mean you need to start accepting requests right away! Take a look at the classic HTTP server example:

var http = require('http');

var server = http.createServer(function (req, res) {
 // ... logic to handle requests ...
});

server.listen(8000);

You can do anything you like before calling server.listen, including whatever configuration tasks you need. Assuming those tasks are asynchronous, you can start the server in the callback:

var http = require('http');


var server = http.createServer(function (req, res) {
 // ... logic to handle requests ...
});


// Set up your mongo DB and grab a collection, and then...
myMongoCollection.find().toArray(function(err, results) {

   // Do something with results here...

   // Then start the server
   server.listen(8000);

});

Upvotes: 9

Related Questions