Nodejs – 设置不同的api版本

我有以下设置API版本1,将通过中间件validation令牌

var app = express(); // Auth Middleware - This will check if the token is valid // Only the requests that start with /api/version1/* will be checked for the token. // Any URL's that do not follow the below pattern should be avoided unless you // are sure that authentication is not needed app.all('/api/version1/*', [require('./middleWare/validateMyRequest')]); app.use('/', require('./myModal')); app.all('/*', function(req, res) { res.sendFile('/public/index.html', { root: __dirname }); }); 

在这里我想设置另一个API版本2在服务器configuration中,我不需要validation令牌使用中间ware.I已尝试使用以下configuration

 app.use('/api/version2/*', require('./myModal')); 

当我尝试与API版本2 Baseurl。 它总是validation令牌并redirect到login页面。 请告诉我缺less这里设置API版本2?

把你的两个独立的API放在他们自己的路由器上。 然后你可以在每个路由器上有不同的中间件,一个做一种validation/authentication,另一个做不同的事情。

 app.use('/api/version1', router1); app.use('/api/version2', router2); router1.use(require('./middleWare/validateMyRequest')); router1.get('/whatever', function(req, res) { // handle /api/version1/whatever here }); router2.get('/something', function(req, res) { // handle /api/version2/something here });