简体   繁体   中英

express.static + middlware = 404

This works:

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

However as soon as I add middleware, the page can't be found.

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

With the middleware in place I get a 404. What am I missing here?

app.use only takes one parameter. you need to split it into two app.use() s.

You should change the middleware to this :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM