javascript - nodejs concurrent requests to express controller get records from mongodb wait each other -
i testing controllers see response times in nodejs express module:
1) test connecting mongodb , getting first record using mongoose. related codes:
var mongoose = require('mongoose'); var schema = mongoose.schema; var scoredataschema = new schema({ _id: schema.types.string, x: schema.types.number, y: schema.types.number, z: schema.types.number, // level r: schema.types.mixed }, { collection: 'scoredata', versionkey: false }); scoredataschema.index({ x: 1, y: 1, z: 1}, { unique: true }); var scoredata = mongoose.model('scoredata', scoredataschema); module.exports = scoredata; and controller is:
var express = require('express'), router = express.router(), scoredata = require('../common/models/scoredata'); module.exports = function(app) { app.use('/', router); }; router.get('/mongotest', function(req, res, next) { scoredata.findone({}, '-_id r', function(err, doc) { res.end(json.stringify(doc.r)); }); }); 2) 1 controller request in browser: 
3) test see response times settimeout. sending wait time controller , controller returns constant string.
var express = require('express'), router = express.router(); module.exports = function(app) { app.use('/', router); }; router.get('/mongotest/:time', function(req, res, next) { var waittime = req.params.time; settimeout(function(){ res.end(fixedstr); }, parseint(waittime)); });
question: see in first image seems how controllers waits each other, or related single thread problem in nodejs. mongodb requests should async settimeout function? see in second image sure single request takes 85ms total.
thanks,

Comments
Post a Comment