guide.md
This file
Path
docs/guide.md
180 lines · 5.1 KiB · Markdown
Last changed by
More docsTj Holowaychukbefore adoptionbedff32
History of this fileTake it
Started guideTj Holowaychukbefore adoption9e3a0c6
line 1why, one step back1Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
line 2why, one step back2### Creating An ApplicationAdded configuration sectionTj Holowaychukbefore adoption83d2555
lines 3–4why, one step back34The _express.Server_ now inherits from _http.Server_, howeverMoved config docs upTj Holowaychukbefore adoption6e8413f
lines 5–6why, one step back5follows the same idiom by providing _express.createServer()_ as shown below. This means6that you can utilize Express server's transparently with other libraries.Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 7–15why, one step back78 var app = require('express').createServer();9 10 app.get('/', function(req, res){11 res.send('hello world');12 });13 14 app.listen(3000);15Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 16–56why, one step back16### Configuration1718Express supports arbitrary environments, such as _production_ and _development_. Developers19can use the _configure()_ method to setup needs required by the current environment. When20_configure()_ is called without an environment name it will be run in _every_ environment 21prior to the environment specific callback.2223In the example below we only _dumpExceptions_, and respond with exception stack traces24in _development_ mode, however for both environments we utilize _methodOverride_ and _bodyDecoder_.2526 app.configure(function(){27 app.use('/', connect.methodOverride());28 app.use('/', connect.bodyDecoder());29 });30 31 app.configure('development', function(){32 app.use('/', connect.errorHandler({ dumpExceptions: true, showStack: true }));33 });34 35 app.configure('production', function(){36 app.use('/', connect.errorHandler());37 });3839For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:4041 app.configure(function(){42 app.set('views', __dirname + '/views');43 app.set('views');44 // => "... views directory ..."45 46 app.enable('some feature');47 // same as app.set('some feature', true);48 49 app.disable('some feature');50 // same as app.set('some feature', false);51 });5253### Settings5455Express supports the following settings out of the box:56More docsTj Holowaychukbefore adoptionbedff32
line 57why, one step back57 * _home_ Application base path used with `res.redirect()`Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 58–59why, one step back58 * _views_ Root views directory defaulting to **CWD/views**59 * _view engine_ Default view engine name for views rendered without extensionsAdded more setting docsTj Holowaychukbefore adoption38281e6
lines 60–61why, one step back60 * _reload views_ Reloads altered views, by default watches for _mtime_ changes with61 with a 5 minute interval. Example: `app.set('reload views', { interval: 60000 });`Moved config docs upTj Holowaychukbefore adoption6e8413f
line 62why, one step back62Started guideTj Holowaychukbefore adoption9e3a0c6
lines 63–66why, one step back63### Routing6465Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.66For example we may want to render a user's account for the path _/user/12_, thisRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 67–68why, one step back67can be done by defining the route below. The values associated to the named placeholders,68are passed as the _third_ argument, which here we name _params_.Started guideTj Holowaychukbefore adoption9e3a0c6
lines 69–112why, one step back6970 app.get('/user/:id', function(req, res, params){71 res.send('user ' + params.id);72 });7374A route is simple a string which is compiled to a _RegExp_ internally. For example75when _/user/:id_ is compiled, a simplified version of the regexp may look similar to:7677 \/user\/([^\/]+)\/?7879Literal regular expressions may also be passed for complex uses:8081 app.get(/^\/foo(bar)?$/, function(){});8283Below are some route examples, and the associated paths that they84may consume:8586 "/user/:id"87 /user/128889 "/users/:id?"90 /users/591 /users9293 "/files/*"94 /files/jquery.js95 /files/javascripts/jquery.js9697 "/file/*.*"98 /files/jquery.js99 /files/javascripts/jquery.js100 101 "/user/:id/:operation?"102 /user/1103 /user/1/edit104 105 "/products.:format"106 /products.json107 /products.xml108109 "/products.:format?"110 /products.json111 /products.xml112 /productsRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 113–128why, one step back113114### Passing Route Control115116We may pass control to the next _matching_ route, by calling the _fourth_ parameter,117the _next()_ function. When a match cannot be made, control is passed back to Connect.118119 app.get('/users/:id?', function(req, res, params){120 if (params.id) {121 // do something122 } else {123 next();124 }125 });126 127 app.get('/users', function(req, res, params){128 // do something elseAdded configuration sectionTj Holowaychukbefore adoption83d2555
lines 129–143why, one step back129 });130131### Middleware132133The Express _Plugin_ is no more! middleware via [Connect](http://github.com/extjs/Connect) can be134passed to _express.createServer()_ as you would with a regular Connect server. For example:135136 var connect = require('connect'),137 express = require('express');138139 var app = express.createServer(140 connect.logger(),141 connect.bodyDecoder()142 );143More docsTj Holowaychukbefore adoptionbedff32
line 144why, one step back144### ServerResponse#redirect(url[, status])Added configuration sectionTj Holowaychukbefore adoption83d2555
line 145why, one step back145More docsTj Holowaychukbefore adoptionbedff32
line 146why, one step back146Redirect to the given _url_ with a default response _status_ of 302.Configuration docsTj Holowaychukbefore adoptioncb04364
line 147why, one step back147More docsTj Holowaychukbefore adoptionbedff32
lines 148–180why, one step back148 res.redirect('/', 301);149 res.redirect('/account');150 res.redirect('http://google.com');151 res.redirect('home');152 res.redirect('back');153154Express supports "redirect mapping", which by default provides _home_, and _back_.155The _back_ map checks the _Referrer_ and _Referer_ headers, while _home_ utilizes156the "home" setting and defaults to "/".157158### Server#redirect(name, val)159160For use with `res.redirect()` we can map redirects at the application level as shown below:161162 app.redirect('google', 'http://google.com');163164Now in a route we may call:165166 res.redirect('google');167168We may also map dynamic redirects:169170 app.redirect('comments', function(req, res, params){171 return '/post/' + params.id + '/comments';172 });173174So now we may do the following, and the redirect will dynamically adjust to175the context of the request. If we called this route with _GET /post/12_ our176redirect _Location_ would be _/post/12/comments_.177178 app.get('/post/:id', function(req, res){179 res.redirect('comments');180 });