【发布时间】:2013-11-09 00:34:55
【问题描述】:
我最近才开始使用sails 和nodejs。
我想知道,有没有一种简单的方法可以使用 Sails 中的配置创建全局前缀?或者我需要引入另一个图书馆吗?
我在 config/controller.js 中找到了蓝图前缀配置。似乎应该有一个简单的方法来做到这一点,因为应用程序已经部分支持它......
我正在尝试在我的应用程序拥有的所有路由前面添加类似 /api/v1 的内容。
谢谢。
【问题讨论】:
我最近才开始使用sails 和nodejs。
我想知道,有没有一种简单的方法可以使用 Sails 中的配置创建全局前缀?或者我需要引入另一个图书馆吗?
我在 config/controller.js 中找到了蓝图前缀配置。似乎应该有一个简单的方法来做到这一点,因为应用程序已经部分支持它......
我正在尝试在我的应用程序拥有的所有路由前面添加类似 /api/v1 的内容。
谢谢。
【问题讨论】:
您可以在 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.10 的 Sails.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.config.routes 的手动路由”。
@Edit 07.09.2015 的方法从 2018 年开始仍然可以正常工作(Sails 1.0)
从 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: '',
【讨论】:
如果您在 config/routes.js 中明确定义您的路线,请尝试以下操作:https://stackoverflow.com/a/42797788/5326603
【讨论】: