小编pau*_*oag的帖子

ExpressJS - 如何处理同步请求?请求似乎彼此阻止.

我有以下代码

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 …
Run Code Online (Sandbox Code Playgroud)

asynchronous node.js express

6
推荐指数
1
解决办法
2834
查看次数

标签 统计

asynchronous ×1

express ×1

node.js ×1