restify-by-convention

Routes HTTP requests to the appropriate controller and function by convention.

npm install restify-by-convention
1 downloads in the last week
4 downloads in the last month

restify-by-convention

This is a very new project, so better documentation is to come... I hope. ;-)

For now, here's how to use it with restify:

var restify = require("restify");
var restifybc = require("restify-by-convention");
var server = restify.createServer();
restifybc.configureRoutes("/v1", server, dependencies);
restifybc.configureRoutes("/v2", server, dependencies, { camelCase: true, suffix: "Ctrl" });
restifybc.configureRoutes("/v3", server, dependencies, { suffix: "Controller" });
server.listen(8080);

With this configuration:

  1. For a request for /v1/accounts or /v1/accounts/123 a) The "get" function of the object referenced by "dependencies" will be invoked with a single string parameter of "Accounts" in order to get the controller instance. If the instance is null, then the server will respond with a 404. b) The controller instance will be searched for a function named "del", "get", "post", or "put" based on the HTTP verb. If the function does not exist, then the server will respond with a 405. c) The "del", "get", "post", or "put" function of the controller instance will be invoked depending on the HTTP verb. The parameters provided will be the request object, the response object, and the "next" restify function, just as with the manual method of configuring restify routes. d) The ID of 123 will be assigned to the request parameters field "id" and thus can be accessed by req.params.id.

  2. For a request for /v2/private-messages or /v2/private-messages/321, all of the above will apply EXCEPT that the controller instance will be retrieved by the dependency name "privateMessagesCtrl".

  3. For a request for /v3/fluffy-white-kittens, all of the above will apply EXCEPT that the controller instance will be retrieved by the dependency name "FluffyWhiteKittensController".

Get it? Good.

One final note: Although I did not want to create a dependency in this package on the node-di package (see https://github.com/vojtajina/node-di), I tried to make it easy to use with a node-di injector. Simply provide your Injector instance for the "dependencies" parameter, and the controller dependencies will be resolved from it.

npm loves you