如何在sails中创build全局路由前缀?

我刚刚开始使用sails和nodejs。

我想知道,是否有一种简单的方法来创build一个全局的前缀使用Sails中可用的configuration? 或者我需要带来另一个图书馆?

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

我正在尝试在我的应用程序的所有路线之前获得/ api / v1之类的内容。

谢谢。

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

因此,前缀设置为/api/v1和路由/some ,可以在uri /api/v1/some

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

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

@EDIT 08.08.2014

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

@EDIT 14.08.2014

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

@编辑07.09.2015

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

假设蓝图configuration文件中的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', }); 

从版本0.12.x开始,位于第100行的config / blueprints.js中。如前所述适用相同的规则。 前缀仅适用于蓝图自动漫游,而不是在config / routes.js中手动创build路由。

/*************************************************************************** * * * 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: '', /*************************************************************************** * * * 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 / blueprints.js中的第100行

如果你明确地在config/routes.js定义你的路由,那么试试这个: https : config/routes.js