What is a better way to authenticate some of the routes on Express 4 Router? What is a better way to authenticate some of the routes on Express 4 Router? express express

What is a better way to authenticate some of the routes on Express 4 Router?


You could split your router up into protected/unprotected and call the middleware on the protected routes.

var express = require('express'),    media = express.Router(),    mediaProtected = express.Router();media.get('/', function(req, res) {    // provide results from db});mediaProtected.post('/', function(req, res) {    // This route is auth protected});module.exports = {    protected: mediaProtected,    unprotected: media};

And then you can do

var router = require('./my-router');app.use('/api/route', passport.authenticate('bearer'), router.protected);app.use('/api/route', router.unprotected);