【问题标题】:Express.js routing. How to put a default route before all of my existing routesExpress.js 路由。如何在所有现有路由之前放置默认路由
【发布时间】:2016-01-25 18:45:34
【问题描述】:

我可能用错了这个问题的措辞,所以请多多包涵。所以现在我有一些路线:

本地主机:3000/索引

本地主机:3000/家

localhost:3000/登录

localhost:3000/忘记了

但在 URL 中的每个路由之前,我想要一个这样的客户端名称:

localhost:3000/client/index

localhost:3000/client/home

localhost:3000/client/login

localhost:3000/client/忘记

有没有一种无需手动更改所有路由字符串的好方法?如果我对这个问题的措辞不好,再次抱歉。

【问题讨论】:

    标签: javascript node.js express routing


    【解决方案1】:

    创建一个路由器(router.js),其中定义了所有这些规则。然后在快递应用中引用:

    app.use('/client', require ('./router'));
    

    你的路由器文件:

    var router = require ('express').Router();
    
    router.use('/index', ...);
    ...
    module.exports = router;
    

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 2014-05-05
      • 2011-04-29
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多