【问题标题】:how to create a global route prefix in sails?如何在sails中创建全局路由前缀?
【发布时间】:2013-11-09 00:34:55
【问题描述】:

我最近才开始使用sails 和nodejs。

我想知道,有没有一种简单的方法可以使用 Sails 中的配置创建全局前缀?或者我需要引入另一个图书馆吗?

我在 config/controller.js 中找到了蓝图前缀配置。似乎应该有一个简单的方法来做到这一点,因为应用程序已经部分支持它......

我正在尝试在我的应用程序拥有的所有路由前面添加类似 /api/v1 的内容。

谢谢。

【问题讨论】:

    标签: node.js routing sails.js


    【解决方案1】:

    您可以在 config/controller.js 中将 prefix 属性设置为 /api/v1。但请注意,这只会将前缀添加到蓝图路由(由 Sails 自动生成的路由)。

    因此,将前缀设置为/api/v1 和路由/some,可以通过uri /api/v1/some 访问它。

    但是如果你像这样声明你的路由:"post /someEndPoint": {controller: "someController", action: "someAction"},前缀什么都不做。

    在这种情况下,您必须像这样手动编写它们:post /api/v1/someEndPoint 并将 config/controller.js 中的 actions 属性设置为 false(至少在生产中),以关闭控制器中每个操作的自动生成路由。

    @EDIT 08.08.2014

    以上适用于小于 v0.10Sails.Js 版本。因为我不再使用 Sails,所以我不知道现在框架的当前版本适用什么。

    @EDIT 14.08.2014

    对于sails.js >= 0.10的版本,可以设置前缀的配置文件为config/blueprints.js。它具有与旧版本相同的功能。

    @Edit 07.09.2015

    据我所知,该框架不支持手动定义路由的全局前缀,但由于您仍然可以在配置文件中使用 javascript(因为配置文件是 nodeJs 模块而不是 JSON 文件),您可以轻松调整此功能以根据需要工作。

    假设 prefix 属性在您的蓝图配置文件中设置为 /api,您可以在您的路由中包含此代码。

    var blueprintConfig = require('./blueprints');
    
    var ROUTE_PREFIX = blueprintConfig.blueprints.prefix || "";
    
    // add global prefix to manually defined routes
    function addGlobalPrefix(routes) {
      var paths = Object.keys(routes),
          newRoutes = {};
    
      if(ROUTE_PREFIX === "") {
        return routes;
      }
    
      paths.forEach(function(path) {
        var pathParts = path.split(" "),
            uri = pathParts.pop(),
            prefixedURI = "", newPath = "";
    
          prefixedURI = ROUTE_PREFIX + uri;
    
          pathParts.push(prefixedURI);
    
          newPath = pathParts.join(" ");
          // construct the new routes
          newRoutes[newPath] = routes[path];
      });
    
      return newRoutes;
    };
    
    module.exports.routes = addGlobalPrefix({
    
      /***************************************************************************
       *                                                                          *
       * Make the view located at `views/homepage.ejs` (or `views/homepage.jade`, *
       * etc. depending on your default view engine) your home page.              *
       *                                                                          *
       * (Alternatively, remove this and add an `index.html` file in your         *
       * `assets` directory)                                                      *
       *                                                                          *
       ***************************************************************************/
    
      // '/': {
      //   view: 'homepage'
      // },
    
      /***************************************************************************
       *                                                                          *
       * Custom routes here...                                                    *
       *                                                                          *
       *  If a request to a URL doesn't match any of the custom routes above, it  *
       * is matched against Sails route blueprints. See `config/blueprints.js`    *
       * for configuration options and examples.                                  *
       *                                                                          *
       ***************************************************************************/
    
      'post /fake': 'FakeController.create',
    });
    

    【讨论】:

    • 谢谢。但这有点是我希望避免的……我希望 Sails 有一个简单的配置,允许这样做(与蓝图分开)。但看来我可能不得不手动配置它......
    • @harmlessdragon 是的,我有同样的问题,我认为前缀应该解决它,但没有。所以你必须为每条路由手动编写前缀,我也这样做了......我知道这有点乱,但我看到有一些关于这个的讨论,也为这些路由添加前缀。好的部分是您的路线在同一个文件中,当它们要在前缀上添加“预期功能”时更改它们会更容易
    • 这不适用于 v10 Sails.js,因为不推荐使用配置/控制器
    • 如果您使用sails.js v >= 0.10,您现在应该在config/blueprints.js 中设置前缀。根据该文件中的注释,“这仅适用于蓝图自动路由,不适用于来自 sails.config.routes 的手动路由”。
    • @Edit 07.09.2015 的方法从 2018 年开始仍然可以正常工作(Sails 1.0)
    【解决方案2】:

    从 0.12.x 版开始,它位于第 100 行的 config/blueprints.js 中。适用的规则与前面提到的相同。该前缀仅适用于蓝图自动路由,不适用于 config/routes.js 中手动创建路由。

    /*************************************************************************** * * * An optional mount path for all blueprint routes on a controller, * * including 'rest', 'actions', and 'shortcuts'. This allows you to take * * advantage of blueprint routing, even if you need to namespace your API * * methods. * * * * (NOTE: This only applies to blueprint autoroutes, not manual routes from * * 'sails.config.routes') * * * ***************************************************************************/ // prefix: '',

    【讨论】:

      【解决方案3】:

      如果您在 config/routes.js 中明确定义您的路线,请尝试以下操作:https://stackoverflow.com/a/42797788/5326603

      【讨论】:

        猜你喜欢
        • 2018-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-31
        • 2015-03-08
        • 2013-01-26
        • 2013-08-25
        • 1970-01-01
        相关资源
        最近更新 更多