【发布时间】:2014-02-10 20:31:51
【问题描述】:
我有以下代码
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");
});
我发现当我在 localhost/a 上获取时,大约需要 2.3 秒才能完成。这并不奇怪,因为它从数据库中获取了相当多的数据。但是我发现如果在 /a 加载时获取 /b,b 将不会显示。就好像对 /a 的调用阻止了对 /b 的调用。
这是 express 应该如何工作的吗?我一直假设各个路由是异步的,因为它们接受回调,但似乎 express 一次只能处理一个请求。在调用 res.end() 之前,不会处理其他请求。我是否缺少任何我需要做的配置?
作为参考,这是我连接猫鼬的方式
mongoose.connect(dbConnectionString, {server:{poolSize:25}});
这是我的http服务器初始化部分
http.globalAent.maxSockets = 20; // or whatever
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
编辑:这是卡片模型和相关架构 + 函数的代码
//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);
}
【问题讨论】:
标签: node.js asynchronous express