首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nodejs -设置不同的api版本

Nodejs -设置不同的api版本
EN

Stack Overflow用户
提问于 2017-04-03 12:10:57
回答 1查看 39关注 0票数 1

我有以下version1应用程序接口设置,它将通过中间件验证令牌

代码语言:javascript
复制
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 version2,在这个配置中,我不需要使用中间件验证token。我已经尝试了以下配置

代码语言:javascript
复制
app.use('/api/version2/*', require('./myModal'));

当我尝试使用version2接口使用baseurl时。它总是验证令牌并重定向到登录页面。请告知我在设置version2接口时遗漏了什么?

EN

回答 1

Stack Overflow用户

发布于 2017-04-03 12:20:09

将您的两个单独的API分别放在各自的路由器上。然后,您可以在每个路由器上使用不同的中间件,一个执行一种验证/验证,另一个执行不同的操作。

代码语言:javascript
复制
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
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43176366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档