2017-04-03 60 views
1

我有以下设置的API VERSION1将通过中间件验证令牌的NodeJS - 设置不同的API版本

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

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

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

当我尝试与API版本2的baseurl。它总是验证令牌并重定向到登录页面。请告知我在这里想要设置API版本2?

回答

1

将两个独立的API分别放在自己的路由器上。然后,您可以在每个路由器上使用不同的中间件,一种做一种验证/认证,另一种做不同的事情。

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