Categories
asynchronous express node.js

ExpressJS – How to handle simultaneous requests? Requests seem to block one another.

I have the following piece of code

var express = require('express');
var routes = require('./routes');
var http = require('http');
...
app.get('/a',function(){
Card.findCards(function(err, result){ //Mongoose schema
res.send(result); //Executes a query with 9000 records
})
});
app.get('/b', function(req, res){
res.send("Hello World");
});

I find that when I make a get on localhost/a, it takes around 2.3 seconds to complete. This isn’t really surprising since it fetches quite a bit of data from the database. However I find that if I GET /b while /a is loading, b will not display. It is as if the call to /a is blocking the call to /b.

Is this how express is supposed to work? I’ve always operated on the assumptions that individual routes are asynchronous since they take in callbacks but it seems like express can only process one request at a time. Until res.end() is called, no other request gets processed. Am I missing any configuration that I need to do?

For reference, this is how I connect to mongoose

mongoose.connect(dbConnectionString, {server:{poolSize:25}});

And this is my http server initialization part

http.globalAent.maxSockets = 20; // or whatever
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

EDIT: Here is the code for the Card Model and associated schema + functions

//Card.js
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var CardSchema = new Schema({
_id : {type: String},
stores : [{
store: {type: Schema.Types.ObjectId, ref:'StoreModel', required: true}
, points: {type: Number, required: true}
}]
});
exports.findCards = function(callback){
var query = Card.find({}, callback);
}