ExpressJS App.use路由序列
我在app.js
有以下内容:
app.use(express.static(path.join(__dirname, 'public'))); app.use(function(req, res, next) { console.log('app.use'); ... next(); }); app.use(app.router);
公用文件夹有子文件夹images
, css
和js
。
在app.use(app.router);
,我有从Amazon S3返回图像的路由定义。
app.get('/images/:type/:id', image);
问题是,当一个页面包含图像, app.use
被调用两次。 如何预防呢? 我想忽略/images/*
调用app.use(function(req, res, next) {});
一般Express使用中间件连接体系结构。 每个中间件都接受下一个如果被调用的函数将stream传给下一个中间件。 所以,理论上如果你错过了,你可以存档你想要的东西:
app.use(function(req, res, next) { // check if the route matches amazon file if(...amazon file...) { // serve image } else { next(); } }); app.use(express.static(path.join(__dirname, 'public')));