Explore app structure
Router
Let's start with routes because this is the first place in the compound stack where request handling happens. In other words, when you open http://localhost:3000/lists in your browser, the router should decide what part of the application should handle this request. Routes are configuration rules that explain to the application what paths your application can handle.
Routes are listed in the file
config/routes.js
which looks like this:exports.routes = function (map) {
map.resources('lists');
map.all(':controller/:action');
map.all(':controller/:action/:id');
};
The line
Note how the line "map.resources('lists');" generates a collection of predefined routes.
map.resources('lists');
was added by the list scaffolding generator. The command compound routes
will generate a table that shows all the routes that are available to the app.Note how the line "map.resources('lists');" generates a collection of predefined routes.
lists GET /lists.:format? lists#index
lists POST /lists.:format? lists#create
new_list GET /lists/new.:format? lists#new
edit_list GET /lists/:id/edit.:format? lists#edit
list DELETE /lists/:id.:format? lists#destroy
list PUT /lists/:id.:format? lists#update
list GET /lists/:id.:format? lists#show
ALL /:controller/:action undefined#undefined
ALL /:controller/:action/:id undefined#undefined
In this table the first column shows the route helper name, the second describes the method (aka verb) for the route, the third describes the route itself, and the last column shows the controller#action that handles the request. As you can see, a route is a pattern that defines what a resource request will look like in the address bar of a browser. When a browser asks the server for a route like /lists/1/ the router will test it against these patterns to determine how to handle it.
Note that the word Request has special meaning in Node programming.
It refers to the object created whenever a browserrequests
the server do anything. To anyone visiting your app in a browser the request is represented by the address in the address bar. To your app the request is represented by thereq
object. That object contains several properties including the address that the browser requested.
To make the process of including routes in pages easier, Compound includes router helpers. The helper name should be used to generate paths, and all route helpers are available as methods on the
pathTo
object. These next examples show the output of calling the list router helpers:pathTo.lists() // '/lists'
pathTo.lists({format: 'json'}); // '/lists.json'
pathTo.list(1); // '/lists/1'
pathTo.list(1, {format: 'json'}); // '/lists/1.json'
pathTo.list({id: 1, format: 'json'}); // '/lists/1.json'
pathTo.new_list(); // '/lists/new'
pathTo.edit_list(1); // '/lists/1/edit'
pathTo.edit_list('my-list'); // '/lists/my-list/edit'
By default path helpers are generated with underscores as word separators. This behavior will be changed in future versions and it's highly recommended to add the line map.camelCaseHelperNames = true; to the top of the router file to generate the names in camelCase:newList
andeditList
instead ofnew_list
andedit_list
No comments:
Post a Comment