node.js - NodeJS、Express 和 Mongoose : Calls are intermittantly slow

标签 node.js mongodb express

我有一个非常简单的 nodeJS 应用程序。由于某种原因,服务器的响应时间差异很大。

这是我的 app.js:

var express = require('express');
var http = require('http');
var path = require('path');
var Models = require('./schema/schema.js');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.static(path.join(__dirname, 'public')));

app.use(function(req, res, next) {
    var start = Date.now();

    res.on('header', function() {
        var duration = Date.now() - start;
        var time = Date.now() - req.start;

        fs.appendFile("times.txt", time+"\n", function (err) {});

    });
    next();
});
app.use(app.router);



app.get("/tags", function(req, res) {
    var query = Models.Tag.find({}).sort({'popularity': -1}).limit(200);
    query.exec(function(err, tags) {
        res.status(200);
        res.send(tags);
    });
}

数据总是正确返回,但这里是我的响应时间,由“ header ”函数测量:

19
11
13
6
10
10
8
9
2
62449
57862
24919
9975
11
17
21116
10
3
2
2
13

大多数都小于 10 毫秒,但经常有一些超过一分钟。怎么回事?

架构:

var tagSchema = new mongoose.Schema({
    name : {
        type: String,
        trim: true
    },
    popularity :{
        type:Number, default:0
    },
    created_date: {
        type:Date, default:Date.now
    },
    last_update: {
        type:Date, default:Date.now
    }
});

最佳答案

您是否尝试过使用托管的 mongo 服务器(例如 mongohq)来查看是否是本地 IO 延迟问题?这么小的数据库应该不会有任何问题,除非你正在做一些疯狂的事情,而你显然没有这样做。

关于node.js - NodeJS、Express 和 Mongoose : Calls are intermittantly slow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22824713/

相关文章:

ruby-on-rails-3 - Resque 工作人员 : invalid operator: $oid

java - MongoDB - 合并两个 DBObjects

node.js - Nodejs 和 Expressjs

javascript - 客户端找不到 socket.io.js

node.js - express 4 : Storing db instance

javascript - Mongoose near(...) 对 2dsphere 索引字段的查询未返回有效结果

javascript - 错误 : listen EACCES 0. 0.0.0 :80 OSx Node. js

linux - Node.JS 中的域名

mongodb - 使用日期聚合进行 Mongo 查询

node.js - IIS、Node.js 和带有 IISNode 的 Web 应用程序未使用虚拟目录正确配置