2017-04-03 59 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 
});