pau*_*oag 6 asynchronous node.js express
我有以下代码
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");
});
Run Code Online (Sandbox Code Playgroud)
我发现当我使用localhost/a时,需要大约2.3秒才能完成.这并不奇怪,因为它从数据库中获取了相当多的数据.但是我发现如果在加载/ a时我得到GET/b,b将不会显示.好像对/ a的调用阻止了对/ b的调用.
请问快递应该如何运作?我一直在假设各个路由是异步的,因为它们接受回调但看起来像express一次只能处理一个请求.在调用res.end()之前,不会处理任何其他请求.我错过了我需要做的任何配置吗?
作为参考,这是我连接到mongoose的方式
mongoose.connect(dbConnectionString, {server:{poolSize:25}});
Run Code Online (Sandbox Code Playgroud)
这是我的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'));
});
Run Code Online (Sandbox Code Playgroud)
编辑:这是卡模型和相关模式+函数的代码
//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);
}
Run Code Online (Sandbox Code Playgroud)
我会尽力 :)
Afaik Node.js 在一般意义上不是异步的,它只是在某种意义上是非阻塞的,如果一个连接什么都不做,那么另一个执行某事的连接不会被该不执行任何操作的连接阻塞,
需要大量reactor cpu时间的事情(比如加载大量数据)会阻塞事件循环,尝试以较小的块获取数据
归档时间: |
|
查看次数: |
2834 次 |
最近记录: |