Node.js ExpressJS应用程序。使用路线顺序

Node.js ExpressJS应用程序。使用路线顺序,node.js,express,routes,Node.js,Express,Routes,我在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); public文件夹有子文件夹images,css和js app.use(app.router)之后,我有从AmazonS3返回图像的路

我在
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);
public
文件夹有子文件夹
images
css
js

app.use(app.router)之后,我有从AmazonS3返回图像的路由定义

app.get('/images/:type/:id', image);

问题是,当页面包含图像时,
app.use
被调用两次。如何预防?我想忽略调用
app.use(函数(req,res,next){}的
/images/*

在general Express中使用中间件连接体系结构。每个中间件都接受下一个函数,如果调用该函数,则将流传递给下一个中间件。因此,理论上,如果您错过了它,您可以归档您想要的:

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')));

每张图片两次还是每页两次?这里您的S3图像不是静态的,所以一个请求将用于页面,另一个请求将用于S3图像。