为什么我的中间件是在路由执行完之后执行?
发布于 12 年前 作者 xlkmuu 5504 次浏览 最后一次编辑是 8 年前

console里显示,先执行的GET请求,再是加载js,图片什么的,最后在再执行中间件。 是不是我的中间件顺序错了?请大家看下:

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
//配置文件
var config = require('./config').config;
//获取地址
// host: http://127.0.0.1
var urlinfo = require('url').parse(config.host);
config.hostname = urlinfo.hostname || config.host;




var app = express();
app.configure(function(){
    // all environments
    app.set('port', process.env.PORT || config.port);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.favicon());
    //console.log设置展示时间
    app.use(express.logger('dev'));
    //是Connect内建的middleware,设置此处可以将client提交过来的post请求放入request.body中。
    app.use(express.bodyParser());
    //methodOverride放在bodyParser下方用于包含表单值的req.body
    app.use(express.methodOverride());
    //添加cooket和session
    app.use(express.cookieParser());
    app.use(express.session({
        secret: config.session_secret
    }));
    //添加路由机制
    app.use(app.router);
    //设置静态文件位置
    app.use(express.static(path.join(__dirname, 'public')));

});

/*
// 错误处理机制在开发模式下将错误输出出来
if ('development' == app.get('env')) {
    app.use(express.errorHandler());
}*/

//设置生产环节和开发环境
var maxAge = 3600000 * 24 * 30;
var staticDir = path.join(__dirname, 'public');
app.configure('development', function () {
    app.use(express.static(staticDir));
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function () {
    app.use(express.static(staticDir, { maxAge: maxAge }));
    app.use(express.errorHandler());
    app.set('view cache', true);
});


// set static, dynamic helpers 设置config
app.use(function (req, res, next) {
    console.log('config');
    res.locals.config = config;
    return next();
});

app.use(require('./controllers/sign').auth_user);


//设置防范csrf攻击
// custom middleware
var csrf = express.csrf();
app.use(function (req, res, next) {
    console.log('csrf');
    csrf(req, res, next);
});

//检测完csrf后设置_csrf为空。
app.use(function(req, res, next){
    res.locals.csrf = req.session ? req.session._csrf : '';
    //res.locals.req = req;
    //res.locals.session = req.session;
    console.log('csrf is ' + res.locals.csrf);
    next();
});

routes(app);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
3 回复
POST /login 302 3ms - 58b
GET / 200 4ms - 5.23kb
GET /stylesheets/bootstrap.css 304 1ms
GET /stylesheets/bootstrap-responsive.css 304 1ms
GET /stylesheets/darkstrap.css 304 2ms
GET /javascripts/jquery.js 304 2ms
GET /javascripts/bootstrap.js 304 3ms
GET /Page 200 6ms - 6.54kb
GET /stylesheets/bootstrap.css 304 2ms
GET /stylesheets/bootstrap-responsive.css 304 7ms
GET /stylesheets/darkstrap.css 304 2ms
GET /javascripts/jquery.js 304 4ms
GET /javascripts/bootstrap.js 304 3ms
GET /imageAlbum/16 200 6ms - 5.52kb
GET /stylesheets/bootstrap.css 304 2ms
GET /stylesheets/bootstrap-responsive.css 304 2ms
GET /stylesheets/darkstrap.css 304 3ms
GET /javascripts/jquery.js 304 5ms
GET /javascripts/bootstrap.js 304 6ms
config
auto login
have session user

附上命令行,命令行最下面是执行中间件方法时返回的信息

你把router配置在其他中间件前面了

//添加路由机制
app.use(app.router);

写反了吧,理论上express基于connect,是按定义的顺序执行的。你把rount定义为一个中间件到最后当然就这样了。

回到顶部