Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a global route prefix in sails?

I just recently started using sails and nodejs.

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?

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...

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

Thanks.

like image 686
rcheuk Avatar asked Oct 29 '13 17:10

rcheuk


2 Answers

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).

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

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

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 08.08.2014

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

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 07.09.2015

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.

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',
});
like image 71
eAbi Avatar answered Oct 28 '22 12:10

eAbi


As of version 0.12.x, this is located in config/blueprints.js on line 100. The same rules apply as previously mentioned. The prefix only applies to the blueprint autoroutes, not manually create routes in 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: '', <----- line 100 in config/blueprints.js

like image 27
johnazre Avatar answered Oct 28 '22 10:10

johnazre