localhost上非常长的TTFB(Node / Express)

时间:2015-09-10 22:42:31

标签: javascript node.js performance express server

我在 localhost 节点/快速服务器上遇到了极长的TTFB。

平均每个请求(包括静态资源)TTFB大约为1秒,偶尔最多7秒(我猜这不正常)

什么可能导致这个长TTFB以及如何改进?

配置快递错误可能是我的错吗?

这是我的app.js:



var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var favicon = require('serve-favicon');
var morgan = require('morgan')


//Router files
var all = require('./api/all');
var admin = require('./api/admin');
var student = require('./api/student');
var teacher = require('./api/teacher');
var course = require('./api/course');
var forum = require('./api/forum');
var group = require('./api/group');
var board = require('./api/board');

// App Setup
var app = express();
// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');


app.use(morgan(':remote-addr [:date[clf]] :user :method :url :status :response-time ms',{
  skip: function (req, res) {
      //var noUser = (req.AV.user === undefined);
      //return noUser;
      return true;
  }
}));

app.use(favicon(__dirname + '/public/favicon.ico'));


// Load Cloud Code
//app.use(cloud);


//Express Middleware
app.use(methodOverride('_method'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));



app.use(express.static(__dirname+'/public'));
//The Route
app.get('/', function(req, res) {
    res.send('./public/index.html');
});

//Routers
app.use('/api',all);
app.use('/api/admin',admin);
app.use('/api/student',student);
app.use('/api/teacher',teacher);
app.use('/api/course',course);
app.use('/api/forum',forum);
app.use('/api/group',group);
app.use('/api/board',board);



//404 Handler
app.use(function(req, res, next) {
    //console.log("NOT FOUND: "+req.url);
    var err = new Error('Not Found');
    err.status = 404;
    res.render('notfound.jade');
    //next(err);
});
// error handlers for different env
if (app.get('env') === 'development') {
    console.log("dev env");
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        if(res.status===500){
            console.log(err);
            //console.log(err.stack.split("\n"));
        }
        res.send({
            message: err.message,
            error: err
        });
    });
}
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    console.log(err.message);
    res.send({
        message: err.message,
        error: {}
    });
});

module.exports = app;




ps:我使用的是节点4.0.0和最新的chrome

0 个答案:

没有答案