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

孤街浪徒 提交于 2019-12-03 00:48:37

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!