在Express中分组路线

时间:2016-08-11 10:41:55

标签: node.js express middleware

我们可以在Laravel中将这样的路线分组:

Route::group("admin", ["middleware" => ["isAdmin"]], function () {

     Route::get("/", "AdminController@index");
     Route::post("/post", ["middleware" => "csrf", "uses" => "AdminController@index");

});

基本上,admin组中定义的所有路由都会自动获取isAdmin中间件和组名。例如,post端点侦听admin/post而不是/post

有没有办法用Express做同样的事情?它会很棒,因为我的Laravel路线过去很干净,而我的Express路线有点乱/重复。

这是我目前在Express上的routes.js。

app.get("/admin", [passportConfig.isAuthenticated, passportConfig.isAdmin], AdminController.index);
app.post("/admin", [passportConfig.isAuthenticated, passportConfig.isAdmin], AdminController.postIndex);

谢谢。

6 个答案:

答案 0 :(得分:5)

您可以使用app.use() - https://expressjs.com/en/guide/using-middleware.html#middleware.application

app.use("/admin",[passportConfig.isAuthenticated, passportConfig.isAdmin],AdminController)


// AdminController:
var express = require('express');
var router = express.Router();
router.get('/', AdminController.index);
// etc...
module.exports = router

https://expressjs.com/en/guide/routing.html#express-router

答案 1 :(得分:4)

在您想要做的每个小组之前使用:

app.use('/admin', AdminMiddleware);
app.get('/admin/route1', ... 
app.get('/admin/route2', ... 

app.use('/user', UserMiddleware);
app.get('/user/route1', ...
app.get('/user/route2', ...

答案 2 :(得分:2)

var app = require('express');
require('express-group-routes');

app.group("/api/v1", (router) => {
    router.get("/login", loginController.store); // /api/v1/login 
});
  

如果您不想添加前缀但仍需要对某些路线进行分组,您可以保留第一个参数并直接进入该功能:

 var app = require('express');
require('express-group-routes');

app.group((router) => {
    router.use(middleware);
});

答案 3 :(得分:2)

从express 4开始,您可以定义和组成路由器

const app = require('express');
const adminRouter = app.Router();

adminRouter.use(isAdmin);
adminRouter.get('/', admin.index); /* will resolve to /admin */
adminRouter.post('/post', csrf, admin.index); /* will resolve to /admin/post */

app.use('/admin', adminRouter); 

希望有帮助!

答案 4 :(得分:0)

我刚刚编写了此模块来解决您的问题:https://github.com/benjamin658/express-inject-middleware

您可以将中间件分组为一个数组,并将其传递给express-inject-middleware ...

例如:

import express from 'express';
import { injectMiddleware } from 'express-inject-middleware';

const app = express();

const authMiddleware = (req, res, next) => {
  // some auth logic...
};

const fooMiddleware = (req, res, next) => {
  // some foo logic
}

const barMiddleware = (req, res, next) => {
  // some bar logic
}

app.use(injectMiddleware(
  [
    authMiddleware,
    fooMiddleware,
  ],
  [
    // Passing the app.[METHOD] as the parameter.
    app.get('/secrets', (req, res, next) => res.send('secrets'));

    // Mount barMiddleware itself
    app.post('/secrets', barMiddleware, (req, res, next) => res.send('ok'));
  ],
));

这是结果:

app.get('/secrets', authMiddleware, fooMiddleware, (req, res, next) => res.send('secrets'));
app.post('/secrets', authMiddleware, fooMiddleware, barMiddleware, (req, res, next) => res.send('ok'));

答案 5 :(得分:0)

我找到了更好的解决方案,您可以按照此方法运行,效果很好

路由文件route/user.js

var express = require('express')
var router = express.Router()
const authMiddleware = require('../middleware/auth')

express.application.prefix = express.Router.prefix = function(path, middleware, configure) {
    configure(router);
    this.use(path, middleware, router);
    return router;
}


router.prefix('/user', authMiddleware, async function (user) {
    user.route('/details').get(function(req, res) {
        res.status(201).send('Hello this is my personal details')
    }); //also you can use controller method if you have any
});

module.exports = router //make sure you have to import/use this route in main/server js