使用ExpressJS减少第一个字节时间?

时间:2015-06-24 01:10:53

标签: node.js express

我正在试图弄清楚如何加速一些。设置呈现模板的基本路由通过ping响应~280ms。如果我在处理程序中放入一条日志消息,它会在浏览器刷新时立即显示,但实际响应需要300毫秒才能发回。

是否有某些设置可能会对此产生影响?

这是我的路线:

app.get '*', (req, res) ->
    if !req.user
        return res.redirect('/login')
    res.render 'index'

这是我的所有中间件和服务器配置:

express = require 'express'
compression = require 'compression'
cookieParser = require 'cookie-parser'
bodyParser = require 'body-parser'
session = require 'express-session'
morgan = require 'morgan'
winston = require 'winston'
settings = require './settings'
user = require './user'

passport = require 'passport'
GoogleStrategy = require('passport-google-oauth').OAuth2Strategy
# Used for persisting sessions only
RedisStore = require('connect-redis')(session)

logger = new winston.Logger({
    transports: [
        new winston.transports.Console(),
        new winston.transports.File({filename: settings.PROJECT_DIR + 'logs.log'})
    ]
})

app = express()

# Misc
app.use morgan('dev')

# Express settings
app.set 'views', settings.PROJECT_DIR + 'views'
app.set 'view engine', 'jade'

oneDay = 86400000
app.use express.static(settings.PROJECT_DIR + "public", {maxAge: oneDay})
app.use compression()

# Parsers
app.use cookieParser('redacted')
app.use bodyParser()

# Users
app.use session
    secret: 'redacted'
    store: new RedisStore
    resave: false
    saveUninitialized: true
    cookie: {maxAge: 2592000000}

app.use passport.initialize()
app.use passport.session()

passport.use new GoogleStrategy
    clientID: 'redacted',
    clientSecret: 'redacted',
    callbackURL: "redacted"
    , (accessToken, refreshToken, profile, done) ->
        user.findOrCreate profile, (err, user) ->
            return done(err, user)

passport.serializeUser (user, done) ->
    done(null, user.id)

passport.deserializeUser (id, done) ->
    user.findById id, (err, user) ->
        done(err, user);

server = require('http').createServer app
routes = require('./routes') app

server.listen(9100)

我在服务器前面有一个nginx反向代理,但是直接通过ip和port访问没有显示任何变化。

0 个答案:

没有答案