向ExpressJS app.use()添加一个exception

我正在开发一个NodeJS项目,并使用Express作为我的路由框架。

我在我的网站上有一个registry格,还有一个login表单,它们分别向/users/register/login )发送请求。 但是,我希望能够有/users/:userID作为路由来查看不同用户的configuration文件,但当然,这条路线意味着我有一个session_id为每个login用户。

我的问题是,我怎么能使用app.use('/users', checkForSessionId) ,而不应用它来注册和login?

这是你需要使用中间件的地方

app.js

 var users = require('./routes/user'); app.use('/users', users); 

./routes/user.js

 var express = require('express'); var router = express.Router(); function checkForSessionId(req, res, next){ //if no valid session // return res.status(401).json("not authorised"); //else next(); } router.get('/:userId', checkForSessionId, function(req, res){ //this is a route which requires authentication }) router.post('/register', function(req, res){ //authentication is not necessary }) module.exports = router;