Solve Nodejs with Express, Mongoose asynchronous process problem solving method

Source: Internet
Author: User
Tags foreach mongodb require

1. Intro

At the node end, the Express HTTP boot and Mongoose MongoDB connections are asynchronous, so it is encountered that when the Web site is opened, the database is not connected, causing a query error, or encountering the database already connected, but the HTTP service has failed to start.

There is a more complicated use scenario: first, link the database, then middleware (such as query fixed configuration, such as site title, Administrator information, oauth configuration, etc. do not need to change the configuration, but need to store the configuration of the database, because the source code is open on the GitHub, the current author's needs are such) , and finally starts the HTTP server.

This startup process, although somewhat troublesome, is often present in the case of node-side asynchrony. For example (here is just a sample), query 3 users, and then query the number of articles published by the 3 users, as well as the topics of interest to these 3 users. Does it look complicated?

Above, if you think it's easy, then you can stop.

2, using HOWdo to handle asynchronous serial

2.1, Express + Mongoose

If you start with just these two, then you don't have to worry about it, just like this:

var HOWdo = require (' HOWdo ');
/**
* Start the HTTP service
* @param callback {Function} callback
*/
var express = function (callback) {
var app = require (' Express ') ();
App.listen (callback);
};
/**
* Connect to the database
* @param callback {Function} callback
*/
var mongoose = function (callback) {
var mongoose = require (' Mongoose ');
Mongoose.connect (' mongodb://localhost:27017/some-table ');
Mongoose.connection.on (' Connected ', callback);
Mongoose.connection.on (' Error ', callback);
Mongoose.connection.on (' Disconnected ', callback);
};
/**
* Asynchronous Parallel
*/
Howdo.task (Express). Task (Mongoose). Together (function (err) {
if (err) {
Console.error (ERR);
Process.exit (-1);
}
Console.log (");
Console.log (' ######################################################### ');
Console.log (' HTTP server running at 80 ');
Console.log (' ######################################################### ');
Console.log (");
});

2.2, Express+mongoose+middleware

The source code is as follows (very intuitive):

var HOWdo = require (' HOWdo ');
/**
* Materialized Express
* @param callback {Function} callback
*/
var express = function (callback) {
var app = require (' Express ') ();
Callback (null, APP);
};
/**
* Connect to the database
* @param callback {Function} callback
* @param app {Object} Express instance
*/
var mongoose = function (callback, APP) {
var mongoose = require (' Mongoose ');
var proxycallback = function (err) {
Callback.call (Global, err, Next, app);
};
Mongoose.connect (' mongodb://localhost:27017/some-table ');
Mongoose.connection.on (' Connected ', proxycallback);
Mongoose.connection.on (' Error ', proxycallback);
Mongoose.connection.on (' Disconnected ', proxycallback);
};
/**
* Middleware
* @param callback {Function} callback
* @param app {Object} Express instance
*/
var middleware = function (callback, APP) {
var Somemodel = require (' Path/to/model ');
Somemodel.getconfig (function (err, configs) {
if (err) {
Return callback (ERR);
}
App.locals._configs = configs;
Callback (err, app);
});
};
/**
* Start the HTTP service
* @param callback {Function} callback
* @param app {Object} Express instance
*/
var http = function (callback, APP) {
App.listen (callback);
}
/**
* Asynchronous serial
*/
HOWdo
. Task (Express)
. Task (Mongoose)
. Task (Middleware)
. Task (HTTP)
. Follow (function (err) {
if (err) {
Console.error (ERR);
Process.exit (-1);
}
Console.log (");
Console.log (' ######################################################### ');
Console.log (' HTTP server running at 80 ');
Console.log (' ######################################################### ');
Console.log (");
});

3, database query synthesis

Looks more complicated:

var HOWdo = require (' HOWdo ');
var usermode = require (' Path/to/user-model ');
var Postmode = require (' Path/to/post-model ');
var Followmode = require (' Path/to/follow-model ');
/**
* Query 3 Users
* @param callback {Function} complete callback
*/
var getthreeusers = function (callback) {
Usermode.getusers (callback);
};
/**
* Query 1 user's article Information
* @param user {Object} users information
* @param callback {Function} complete callback
*/
var getuserposts = function (user, callback) {
Postmode.getuserposts (user, callback);
};
/**
* Query 1 user's attention information
* @param user {Object} users information
* @param callback {Function} complete callback
*/
var getuserfollows = function (user, callback) {
Followmode.getuserfollows (user, callback);
};
/**
* Read all user's article information
* @param the users {array} user list array
* @param callback {Function} complete callback
*/
var getusersposts = function (users, callback) {
Read the user's article information
Howdo.each (Users, function (index, user, done) {
Getuserposts (user, done);
}). Together (function (err, user0posts, user1posts, user2posts) {
if (err) {
Return callback (ERR);
}
var postslist = Array.prototype.slice.call (arguments, 1);
Write the user's article information
Postslist.foreach (function (posts, index) {
Users[index].posts = posts;
});
Callback (null, users);
});
};
/**
* Read all user's attention information
* @param the users {array} user list array
* @param callback {Function} complete callback
*/
var getusersfollows = function (users, callback) {
Read user's attention information
Howdo.each (Users, function (index, user, done) {
Getuserfollows (user, done);
}). Together (function (err, user0follows, User1follows, user2follows) {
if (err) {
Return callback (ERR);
}
var followslist = Array.prototype.slice.call (arguments, 1);
Write the user's article information
Followslist.foreach (function (follows, index) {
Users[index].follows = follows;
});
Callback (null, users);
});
};
HOWdo
. Task (Getthreeusers)
. Task (function (next, users) {
HOWdo
. Task (function (done) {
Getuserposts (users, done);
})
. Task (function (done) {
Getusersfollows (users, done);
})
Asynchronous parallel
. Together (next);
})
Asynchronous parallel
. Follow (function (err, users) {
if (err) {
Console.error (ERR);
Process.exit (-1);
}
The Users object after assembly
Console.log (users);
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.