【问题标题】:Express.js router - How to process same path with different middlewares?Express.js 路由器 - 如何使用不同的中间件处理相同的路径?
【发布时间】:2021-12-20 11:42:24
【问题描述】:

我有两条路径来注册用户,实际上它们不是两条路径,因为它们具有相同的路径但它们具有不同的中间件。由于它们具有相同的路径,因此其中一个将优先于另一个,而其他路由将不起作用。以下是路线:

// Register a new user as an admin
router.post('/', [auth, authAdmin], async function(req, res, next) { ... });
// Sign in route
router.post('/', async function(req, res, next) { ... });

如何解决这两条路线之间的冲突?

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    您无法使用相同的路径来实现这一点 - 正如您所描述的那样,第一个将优先于另一个。

    您应该为这两个用例定义特定的端点,例如:

    // Register a new user as an admin
    router.post('/register', [auth, authAdmin], async function(req, res, next) { ... });
    // Sign in route
    router.post('/sign-in', async function(req, res, next) { ... });
    

    【讨论】:

    • 嗯,是的。路线路径的命名对我来说看起来不错。谢谢!
    猜你喜欢
    • 2023-02-12
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多