Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 92 lines (62 sloc) 2.464 kB
34abc36 @1602 NPM Package
authored
1 ## Description
2
3 This is simple routing for ExpressJS framework. It allows you to write routes in Ruby on rails 2 style.
4
5 ## Using with Express
6
7 After creating app instead of writing code like `app.get('smth', doSmth);` generate routes like that:
8
4f9c097 @1602 Middleware stack
authored
9 var map = new require('railway-routes').Map(app, handler);
34abc36 @1602 NPM Package
authored
10 map.resources('posts');
11 map.namespace('admin', function (admin) {
12 admin.resources('users');
13 });
14
15 In this example `handler` function will called immediately for each route, accepting three args: `ns`, `controller`, `action` and should return method which will me actually called to server request.
16
17 For example you have two controllers: `posts` and `admin/users` which looks like regular modules:
18
19 `controllers/posts_controller.js`
20
21 exports.show = function (req, res) {
22 res.send('show');
23 };
24
25 exports.edit = function (req, res) {
26 res.send('edit');
27 };
28
29 exports.destroy = function (req, res) {
30 res.send('destroy');
31 };
32
33 ...
34
35 same for `controllers/admin/users_controller.js`
36
37 In that case your handler should be:
38
39 function handler(ns, controller, action) {
40 try {
41 var ctlFile = './controllers/' + ns + controller + '_controller';
42 var responseHandler = require(ctlFile)[action];
43 } catch(e) {}
44 return responseHandler || function (req, res) {
45 res.send('Handler not found for ' + ns + controller + '#' + action);
46 };
47 }
48
49 ## Features
50
51 - resourceful routes
4f9c097 @1602 Middleware stack
authored
52 - generic routes
34abc36 @1602 NPM Package
authored
53 - url helpers
54 - namespaces
55 - custom helper names / paths for resources
56
57 ## Docs
58
59 http://railwayjs.com/routing.html
60
832f869 @olalonde Added some documentation about singleton resources in README.md
olalonde authored
61 ### Singleton resources
62
20a03ce @olalonde Modified the way to add singleton resources. Singleton resources can …
olalonde authored
63 Example:
64
65 map.resource('account');
66
67 Will generate the following routes:
68
69 GET /account account#show
70 POST /account account#create
71 GET /account/new account#new
72 GET /account/edit account#edit
73 DELETE /account account#destroy
74 PUT /account account#update
75
76 Singleton resources can also have nested resources. For example:
77
78 map.resource('account', function(account) {
30bd055 @olalonde When using the path parameter, nested subroutes should be prefixed by…
olalonde authored
79 account.resources('posts');
20a03ce @olalonde Modified the way to add singleton resources. Singleton resources can …
olalonde authored
80 });
832f869 @olalonde Added some documentation about singleton resources in README.md
olalonde authored
81
4f9c097 @1602 Middleware stack
authored
82 ## Example app
83
77c416d Update README.md
Anatoliy Chakkaev authored
84 Check out [example app][1] to deal with middleware, route handling, and generic routes:
4f9c097 @1602 Middleware stack
authored
85
86 git clone git://github.com/anatoliychakkaev/railway-routes-example-app.git
87 cd railway-routes-example-app
88 npm install
89 node app.js
77c416d Update README.md
Anatoliy Chakkaev authored
90
c8138e3 Update README.md
Anatoliy Chakkaev authored
91 [1]: http://github.com/anatoliychakkaev/railway-routes-example-app
Something went wrong with that request. Please try again.