mymotherland
mymotherland

Reputation: 8228

Nodejs - Set up different api version

I have following setup for api version1 which will validate token through middle ware

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

Here i want to set another api version2 in server configuation in which i dont need to validate token using middle ware.I have tried with following config

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

When i tried with baseurl with api version2. It always validate token and redirect to login page. Please advise what i am missing here to set up api version2?

Upvotes: 1

Views: 53

Answers (1)

jfriend00
jfriend00

Reputation: 707158

Put your two separate APIs each on their own router. You can then have different middleware on each router, one that does one kind of validation/auth, the other that does something different.

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

Upvotes: 1

Related Questions