如何在帆中创建全局路由前缀?

rch*_*euk 11 routing node.js sails.js

我刚刚开始使用sails和nodejs.

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

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

我想在我的应用程序的所有路径前面获得/ api/v1之类的东西.

谢谢.

eAb*_*Abi 15

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

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

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

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

@EDIT 08.08.2014

以上适用于Sails.Js小于v0.10的版本.因为我不再使用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',
});
Run Code Online (Sandbox Code Playgroud)

  • 如果你使用sails.js v> = 0.10,你现在应该在config/blueprints.js中设置前缀.根据该文件中的注释,"这仅适用于蓝图自动路由,而不适用于来自`sails.config.routes`的手动路由". (2认同)