express.static + middleware = 404

时间:2013-07-17 02:10:03

标签: node.js express

这有效:

http://localhost:3000/private/test2.html
app.use('/private',express.static(path.join(__dirname, 'private')));

但是,只要我添加中间件,就无法找到该页面。

var secure = function(req,res,next) {
    console.log('in here' + req.url);
    next();
}
app.use('/private',secure,express.static(path.join(__dirname, 'private')));

随着中间件的到位,我得到了404.我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

app.use只接受一个参数。你需要把它分成两个app.use() s。

答案 1 :(得分:0)

您应该将中间件更改为:

app.use(secure);
// use the middleware function

app.use('/private',express.static(path.join(__dirname, 'private')));
// serve static files from private subfolder using 'private/' as  matching prefix
// static should be used at the end as it finishes the response.