我对 api version1 有以下设置,它将通过中间件验证 token
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,其中我不需要使用中间件验证 token 。我尝试过以下配置
app.use('/api/version2/*', require('./myModal'));
当我尝试使用 api version2 的 baseurl 时。它始终验证 token 并重定向到登录页面。请告知我在设置 api version2 时缺少什么?
最佳答案
将两个单独的 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
});
关于javascript - Nodejs - 设置不同的 api 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43176366/