如何在Sails中创建全局路由前缀? [英] how to create a global route prefix in sails?

查看:115
本文介绍了如何在Sails中创建全局路由前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近才开始使用Sails和Node.js。

I just recently started using sails and nodejs.

我想知道,有没有一种简单的方法可以使用Sails中可用的配置来创建全局前缀?还是需要引入另一个库?

I was wondering, is there an easy way to create a global prefix using the configuration available in Sails? Or would I need to bring in another library?

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

I found the blueprint prefix configuration in config/controller.js. It seems there ought to be an easy way to do this since the application already partially supports it...

我正在尝试获取/ api / v1之类的东西

I'm trying to get something like /api/v1 in front of all routes I have for my application.

谢谢。

推荐答案

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

You can set the prefix property to /api/v1 in config/controller.js . But note that this will only add the prefix to the blueprint routes (those automatically generated by Sails).

因此,前缀设置为 / api / v1 和路由 / some ,可以在uri / api / v1 / some 中进行访问。

Thus, with the prefix set to /api/v1 and the route /some, it could be accessed at uri /api/v1/some.

但是,如果您这样声明路线: post / someEndPoint:{controller: someController,action: someAction} ,前缀不执行任何操作。

But if you declare your routes like this: "post /someEndPoint": {controller: "someController", action: "someAction"}, the prefix does nothing.

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

In this case you must write them manually like this: post /api/v1/someEndPoint and set to false the actions property from config/controller.js (at least in production) to turn off automatically generated routes for every action within your controllers.

@EDIT 2014年8月8日

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

The above applies to versions of Sails.Js smaller than v0.10. Because I don't work anymore with Sails, I don't know what applies now to the current version of the framework.

@EDIT 14.08.2014

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

For versions of sails.js >= 0.10, the configuration file where the prefix can be set is config/blueprints.js. It has the same functionality as had for older versions.

@Edit 2015年7月9日

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

As far as I know, the framework doesn't support a global prefix for manually defined routes, but since you can still use javascript in your config files (since the configuration files are nodeJs modules and not JSON files), you can easily tweak this functionality to work as you need to.

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

Supposing that the prefix property is set to /api in your blueprints config file, you can have this code in your routes.

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中创建全局路由前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆