Skip to content
Myr

guide.html

This file

Path

docs/guide.html

684 lines · 19.9 KiB · HTML

Last changed by

Error handling docsTj Holowaychukbefore adoption9fbb9fa

History of this file

Take it

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 1–46why, one step back
1<html>
2 <head>3 <title>Express - node web framework</title>4 <style>5 #header {6 position: absolute;7 top: 10px;8 left: 0;9 padding: 12px 0;10 text-indent: 40px;11 width: 100%;12 border-top: 1px solid rgba(0,0,0,0.7);13 border-bottom: 1px solid rgba(0,0,0,0.7);14 background: rgba(255,255,255,0.1) url(http://www.sencha.com/favicon.ico) no-repeat 15px 50%;15 text-align: left;16 color: #fff;17 }18 #tagline {19 margin-left: 75px;20 margin-bottom: 30px;21 color: rgba(255,255,255,0.7); }22 html {23 background: #1c1c1c url(images/bg.tile.jpg); }2425 body {26 margin: 0;27 padding-bottom: 30px;28 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";29 font-size: 14px;30 line-height: 1.5;31 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);32 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);33 background: url(images/bg.jpg) 50% 0 no-repeat;34 color: #8b8b8b; }3536 * {37 outline: none; }3839 em {40 color: white; }4142 a img {43 border: none !important; }4445 a {46 font-weight: bold;

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

line 47why, one step back
47 text-decoration: none;

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 48–115why, one step back
48 color: white;
49 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;50 -webkit-transition-duration: 0.15s;51 -webkit-transition-timing-function: ease-out; }52 a:hover {53 opacity: 0.8; }5455 h1, h2, h3 {56 margin: 45px 0 0 0;57 color: white;58 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }5960 h3 {61 font-size: 18px; }6263 pre {64 margin: 20px 10px;65 padding: 25px 20px;66 background: rgba(0,0,0,0.5);67 border: 1px solid #323232;68 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);69 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);70 -webkit-border-radius: 5px;71 -moz-border-radius: 5px; }7273 code {74 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }7576 ul {77 margin: 15px 0;78 padding: 0 0 0 35px; }79 ul li {80 margin: 0;81 padding: 2px 0;82 list-style: square; }8384 .sect {85 margin-left: 40px; }8687 #logo {88 display: block;89 margin-left: 30%;90 margin-bottom: 30px;91 width: 194px;92 height: 51px;93 background: url(images/logo.png) 0 0 no-repeat;94 text-indent: -99999px; }95 #logo:hover {96 opacity: 0.7; }97 #logo:active {98 opacity: 0.3; }99100 #ribbon {101 position: fixed;102 top: 0;103 right: 0;104 z-index: 2; }105106 #wrapper {107 width: 100%;108 min-height: 800px;109 background: url(images/top.png) 0 0 repeat-x; }110111 #container {112 margin: 0 auto;113 padding-top: 110px;114 width: 550px; }115

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

lines 116–126why, one step back
116 #toc {
117 position: fixed;118 top: 60px;119 left: 0;120 margin: 0 0 0 15px;121 padding: 15px;122 height: 100%;123 background: rgba(0,0,0,0.2);124 border-right: 1px solid rgba(255,255,255,0.05);125 }126 #toc li {

More docsTj Holowaychukbefore adoptionde81ee7

line 127why, one step back
127 padding: 1px 0;

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

lines 128–132why, one step back
128 list-style: none;
129 }130 #toc li a {131 font-size: 11px;132 }

Started guideTj Holowaychukbefore adoption9e3a0c6

line 133why, one step back
133 #menu {

Added contrib docsTj Holowaychukbefore adoptionf0ba57b

line 134why, one step back
134 margin-left: 65px;

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 135–149why, one step back
135 padding: 0;
136 padding-bottom: 30px; }137 #menu li {138 display: inline;139 list-style: none; }140 #menu li a {141 display: block;142 float: left;143 margin: 0 2px;144 padding: 3px 15px;145 background: rgba(0,0,0,0.2);146 -webkit-border-radius: 8px;147 -moz-border-radius: 8px;148 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);149 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);

Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2

line 150why, one step back
150 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 151–167why, one step back
151 -webkit-transition-duration: 0.15s;
152 -webkit-transition-timing-function: ease-out; }153 #menu li a:hover,154 #menu li a.active {155 background: rgba(0,0,0,0.5); }156 #menu li a:active {157 background: rgba(0,0,0,0.1);158 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);159 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }160 </style>161 </head>162 <body>163 <a href='http://github.com/visionmedia/express'> 164 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 165 </a>166 <div id="header"><strong>Sencha</strong> labs</div>167 <div id="wrapper">

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

lines 168–175why, one step back
168 <div id="container"><ul id="toc">
169<li><a href="#Installation">Installation</a></li>170<li><a href="#Creating-An-Application">Creating An Application</a></li>171<li><a href="#Configuration">Configuration</a></li>172<li><a href="#Settings">Settings</a></li>173<li><a href="#Routing">Routing</a></li>174<li><a href="#Passing-Route-Control">Passing Route Control</a></li>175<li><a href="#Middleware">Middleware</a></li>

Error handling docsTj Holowaychukbefore adoption9fbb9fa

line 176why, one step back
176<li><a href="#Error-Handling">Error Handling</a></li>

More docsTj Holowaychukbefore adoptionde81ee7

lines 177–193why, one step back
177<li><a href="#req-header-key-defaultValue-">req.header()</a></li>
178<li><a href="#req-accepts-type-">req.accepts()</a></li>179<li><a href="#req-param-name-">req.param()</a></li>180<li><a href="#req-flash-type-msg-">req.flash()</a></li>181<li><a href="#req-isXMLHttpRequest">req.isXMLHttpRequest</a></li>182<li><a href="#res-header-key-val-">res.header()</a></li>183<li><a href="#res-contentType-type-">res.contentType()</a></li>184<li><a href="#res-attachment-filename-">res.attachment()</a></li>185<li><a href="#res-sendfile-path-">res.sendfile()</a></li>186<li><a href="#res-download-file-filename-">res.download()</a></li>187<li><a href="#res-send-body-status-headers-status-status-">res.send()</a></li>188<li><a href="#res-redirect-url-status-">res.redirect()</a></li>189<li><a href="#app-set-name-val-">app.set()</a></li>190<li><a href="#app-enable-name-">app.enable()</a></li>191<li><a href="#app-disable-name-">app.disable()</a></li>192<li><a href="#app-configure-env-function-function-">app.configure()</a></li>193<li><a href="#app-redirect-name-val-">app.redirect()</a></li>

Error handling docsTj Holowaychukbefore adoption9fbb9fa

line 194why, one step back
194<li><a href="#app-error-function-">app.error()</a></li>

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

line 195why, one step back
195</ul>

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 196–203why, one step back
196 <a href='http://github.com/visionmedia/express' id='logo'>Express</a>
197 <p id="tagline">198 High performance, high class web development for199 <a href="http://nodejs.org">Node.js</a>200 </p>201 <ul id="menu">202 <li><a href="index.html">Home</a></li>203 <li><a href="guide.html">Guide</a></li>

Added contrib docsTj Holowaychukbefore adoptionf0ba57b

line 204why, one step back
204 <li><a href="contrib.html">Contributing</a></li>

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 205–207why, one step back
205 <li><a href="migrate.html">1.x Migration</a></li>
206 </ul>207<div class='mp'>

Install docsTj Holowaychukbefore adoption63d02f1

lines 208–224why, one step back
208<h3 id="Installation">Installation</h3>
209210<p>npm:</p>211212<pre><code>$ npm install express213</code></pre>214215<p>curl (or similar):</p>216217<pre><code>$ curl -# http://github.com/extjs/Connect/raw/master/install.sh | sh218</code></pre>219220<p>git clone or tarball for <strong>HEAD</strong>:</p>221222<pre><code>$ make install223</code></pre>224

Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2

line 225why, one step back
225<h3 id="Creating-An-Application">Creating An Application</h3>

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 226–227why, one step back
226
227<p>The <em>express.Server</em> now inherits from <em>http.Server</em>, however

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 228–229why, one step back
228follows the same idiom by providing <em>express.createServer()</em> as shown below. This means
229that you can utilize Express server's transparently with other libraries.</p>

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 230–239why, one step back
230
231<pre><code>var app = require('express').createServer();232233app.get('/', function(req, res){234 res.send('hello world');235});236237app.listen(3000);238</code></pre>239

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 240–278why, one step back
240<h3 id="Configuration">Configuration</h3>
241242<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers243can use the <em>configure()</em> method to setup needs required by the current environment. When244<em>configure()</em> is called without an environment name it will be run in <em>every</em> environment245prior to the environment specific callback.</p>246247<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces248in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyDecoder</em>.</p>249250<pre><code>app.configure(function(){251 app.use('/', connect.methodOverride());252 app.use('/', connect.bodyDecoder());253});254255app.configure('development', function(){256 app.use('/', connect.errorHandler({ dumpExceptions: true, showStack: true }));257});258259app.configure('production', function(){260 app.use('/', connect.errorHandler());261});262</code></pre>263264<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>265266<pre><code>app.configure(function(){267 app.set('views', __dirname + '/views');268 app.set('views');269 // =&gt; "... views directory ..."270271 app.enable('some feature');272 // same as app.set('some feature', true);273274 app.disable('some feature');275 // same as app.set('some feature', false);276});277</code></pre>278

More docsTj Holowaychukbefore adoptionde81ee7

lines 279–284why, one step back
279<p>To alter the environment we can set the <em>CONNECT_ENV</em> environment variable,
280or more specifically <em>EXPRESS_ENV</em>, for example:</p>281282<pre><code>$ EXPRESS_ENV=production node app.js283</code></pre>284

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 285–289why, one step back
285<h3 id="Settings">Settings</h3>
286287<p>Express supports the following settings out of the box:</p>288289<ul>

More docsTj Holowaychukbefore adoptionde81ee7

line 290why, one step back
290<li><em>env</em> Application environment set internally, use <em>app.set('env')</em> to utilize</li>

Install docsTj Holowaychukbefore adoption63d02f1

line 291why, one step back
291<li><em>home</em> Application base path used with <em>res.redirect()</em></li>

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 292–293why, one step back
292<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>
293<li><em>view engine</em> Default view engine name for views rendered without extensions</li>

Added more setting docsTj Holowaychukbefore adoption38281e6

line 294why, one step back
294<li><em>reload views</em> Reloads altered views, by default watches for <em>mtime</em> changes with

Fixed reload views exampleTj Holowaychukbefore adoption5fcf971

line 295why, one step back
295 with a 5 minute interval. Example: <em>app.set('reload views', 60000);</em></li>

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 296–298why, one step back
296</ul>
297298

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 299–302why, one step back
299<h3 id="Routing">Routing</h3>
300301<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.302For example we may want to render a user's account for the path <em>/user/12</em>, this

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 303–304why, one step back
303can be done by defining the route below. The values associated to the named placeholders,
304are passed as the <em>third</em> argument, which here we name <em>params</em>.</p>

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 305–351why, one step back
305
306<pre><code>app.get('/user/:id', function(req, res, params){307 res.send('user ' + params.id);308});309</code></pre>310311<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example312when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>313314<pre><code>\/user\/([^\/]+)\/?315</code></pre>316317<p>Literal regular expressions may also be passed for complex uses:</p>318319<pre><code>app.get(/^\/foo(bar)?$/, function(){});320</code></pre>321322<p>Below are some route examples, and the associated paths that they323may consume:</p>324325<pre><code> "/user/:id"326 /user/12327328 "/users/:id?"329 /users/5330 /users331332 "/files/*"333 /files/jquery.js334 /files/javascripts/jquery.js335336 "/file/*.*"337 /files/jquery.js338 /files/javascripts/jquery.js339340 "/user/:id/:operation?"341 /user/1342 /user/1/edit343344 "/products.:format"345 /products.json346 /products.xml347348 "/products.:format?"349 /products.json350 /products.xml351 /products

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 352–368why, one step back
352</code></pre>
353354<h3 id="Passing-Route-Control">Passing Route Control</h3>355356<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,357the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>358359<pre><code>app.get('/users/:id?', function(req, res, params){360 if (params.id) {361 // do something362 } else {363 next();364 }365});366367app.get('/users', function(req, res, params){368 // do something else

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 369–383why, one step back
369});
370</code></pre>371372<h3 id="Middleware">Middleware</h3>373374<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/extjs/Connect">Connect</a> can be375passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>376377<pre><code>var connect = require('connect'),378 express = require('express');379380var app = express.createServer(381 connect.logger(),382 connect.bodyDecoder()383);

More docsTj Holowaychukbefore adoptionbedff32

lines 384–385why, one step back
384</code></pre>
385

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 386–438why, one step back
386<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks:</p>
387388<pre><code>app.use('/', connect.logger({ format: ':method :uri' }));389</code></pre>390391<h3 id="Error-Handling">Error Handling</h3>392393<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,394or passed to <em>next(err)</em>. Below is an example which serves different pages based on our395ad-hoc <em>NotFound</em> exception:</p>396397<pre><code>function NotFound(msg){398 this.name = 'NotFound';399 Error.call(this, msg);400 Error.captureStackTrace(this, arguments.callee);401}402403sys.inherits(NotFound, Error);404405app.get('/404', function(req, res){406 throw new NotFound;407});408409app.get('/500', function(req, res){410 throw new Error('keyboard cat!');411});412</code></pre>413414<p>We can call <em>app.error()</em> several times as shown below.415Here we check for an instanceof <em>NotFound</em> and show the416404 page, or we pass on to the next error handler.</p>417418<pre><code>app.error(function(err, req, res, next){419 if (err instanceof NotFound) {420 res.render('404.jade');421 } else {422 next(err);423 }424});425</code></pre>426427<p>Here we assume all errors as 500 for the simplicity of428this demo, however you can choose whatever you like</p>429430<pre><code>app.error(function(err, req, res){431 res.render('500.jade', {432 locals: {433 error: err434 } 435 });436});437</code></pre>438

More docsTj Holowaychukbefore adoptionde81ee7

line 439why, one step back
439<h3 id="req-header-key-defaultValue-">req.header(key[, defaultValue])</h3>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 440–447why, one step back
440
441<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>442443<pre><code>req.header('Host');444req.header('host');445req.header('Accept', '*/*');446</code></pre>447

More docsTj Holowaychukbefore adoptionde81ee7

line 448why, one step back
448<h3 id="req-accepts-type-">req.accepts(type)</h3>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 449–472why, one step back
449
450<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>451452<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise453the given <em>type</em> is matched by an exact match, and then subtypes. You454may pass the subtype such as "html" which is then converted internally455to "text/html" using the mime lookup table.</p>456457<pre><code>// Accept: text/html458req.accepts('html');459// =&gt; true460461// Accept: text/*; application/json462req.accepts('html');463req.accepts('text/html');464req.accepts('text/plain');465req.accepts('application/json');466// =&gt; true467468req.accepts('image/png');469req.accepts('png');470// =&gt; false471</code></pre>472

More docsTj Holowaychukbefore adoptionde81ee7

line 473why, one step back
473<h3 id="req-param-name-">req.param(name)</h3>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 474–487why, one step back
474
475<p>Return the value of param <em>name</em> when present.</p>476477<ul>478<li>Checks route placeholders, ex: /user/:id</li>479<li>Checks query string params, ex: ?id=12</li>480<li>Checks urlencoded body params, ex: id=12</li>481</ul>482483484<p>To utilize urlencoded request bodies, <em>req.body</em>485should be an object. This can be done by using486the <em>connect.bodyDecoder</em> middleware.</p>487

More docsTj Holowaychukbefore adoptionde81ee7

line 488why, one step back
488<h3 id="req-flash-type-msg-">req.flash(type[, msg])</h3>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 489–506why, one step back
489
490<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>491492<pre><code>req.flash('info', 'email sent');493req.flash('error', 'email delivery failed');494req.flash('info', 'email re-sent');495// =&gt; 2496497req.flash('info');498// =&gt; ['email sent', 'email re-sent']499500req.flash('info');501// =&gt; []502503req.flash();504// =&gt; { error: ['email delivery failed'], info: [] }505</code></pre>506

More docsTj Holowaychukbefore adoptionde81ee7

line 507why, one step back
507<h3 id="req-isXMLHttpRequest">req.isXMLHttpRequest</h3>

More docsTj Holowaychukbefore adoptiondfecbe5

lines 508–515why, one step back
508
509<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header510to see if it was issued by an <em>XMLHttpRequest</em>:</p>511512<pre><code>req.xhr513req.isXMLHttpRequest514</code></pre>515

More docsTj Holowaychukbefore adoptionde81ee7

line 516why, one step back
516<h3 id="res-header-key-val-">res.header(key[, val])</h3>

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 517–529why, one step back
517
518<p>Get or set the response header <em>key</em>.</p>519520<pre><code>res.header('Content-Length');521// =&gt; undefined522523res.header('Content-Length', 123);524// =&gt; 123525526res.header('Content-Length');527// =&gt; 123528</code></pre>529

More docsTj Holowaychukbefore adoptionde81ee7

line 530why, one step back
530<h3 id="res-contentType-type-">res.contentType(type)</h3>

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 531–538why, one step back
531
532<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>533534<pre><code> var filename = 'path/to/image.png';535 res.contentType(filename);536 // res.headers['Content-Type'] is now "image/png"537</code></pre>538

More docsTj Holowaychukbefore adoptionde81ee7

line 539why, one step back
539<h3 id="res-attachment-filename-">res.attachment([filename])</h3>

Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1

lines 540–545why, one step back
540
541<p>Sets the <em>Content-Disposition</em> response header to "attachment", with optional <em>filename</em>.</p>542543<pre><code> res.attachment('path/to/my/image.png');544</code></pre>545

More docsTj Holowaychukbefore adoptionde81ee7

line 546why, one step back
546<h3 id="res-sendfile-path-">res.sendfile(path)</h3>

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

lines 547–555why, one step back
547
548<p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>549550<pre><code>res.sendfile('path/to/my.file');551</code></pre>552553<p><strong>NOTE</strong>: this is <em>not</em> a replacement for Connect's <em>staticProvider</em> middleware,554nor does it perform any security checks, use with caution when using in a dynamic manor.</p>555

More docsTj Holowaychukbefore adoptionde81ee7

line 556why, one step back
556<h3 id="res-download-file-filename-">res.download(file[, filename])</h3>

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

lines 557–569why, one step back
557
558<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>559560<pre><code>res.download('path/to/image.png');561res.download('path/to/image.png', 'foo.png');562</code></pre>563564<p>This is equivalent to:</p>565566<pre><code>res.attachment(file);567res.sendfile(file);568</code></pre>569

More docsTj Holowaychukbefore adoptionde81ee7

line 570why, one step back
570<h3 id="res-send-body-status-headers-status-status-">res.send(body|status[, headers|status[, status]])</h3>

Docs for res.send()Tj Holowaychukbefore adoption5a48248

lines 571–587why, one step back
571
572<p>The <code>res.send()</code> method is a high level response utility allowing you to pass573objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status574code based responses. The following are all valid uses:</p>575576<pre><code> res.send(new Buffer('wahoo'));577 res.send({ some: 'json' });578 res.send('&lt;p&gt;some html&lt;/p&gt;');579 res.send('Sorry, cant find that', 404);580 res.send('text', { 'Content-Type': 'text/plain' }, 201);581 res.send(404);582</code></pre>583584<p>By default the <em>Content-Type</em> response header is set, however if explicitly585assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>586it will not be set again.</p>587

More docsTj Holowaychukbefore adoptionde81ee7

line 588why, one step back
588<h3 id="res-redirect-url-status-">res.redirect(url[, status])</h3>

More docsTj Holowaychukbefore adoptionbedff32

lines 589–602why, one step back
589
590<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>591592<pre><code>res.redirect('/', 301);593res.redirect('/account');594res.redirect('http://google.com');595res.redirect('home');596res.redirect('back');597</code></pre>598599<p>Express supports "redirect mapping", which by default provides <em>home</em>, and <em>back</em>.600The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizes601the "home" setting and defaults to "/".</p>602

More docsTj Holowaychukbefore adoptionde81ee7

lines 603–644why, one step back
603<h3 id="app-set-name-val-">app.set(name[, val])</h3>
604605<p>Apply an application level setting <em>name</em> to <em>val</em>, or606get the value of <em>name</em> when <em>val</em> is not present:</p>607608<pre><code>app.set('reload views', 200);609app.set('reload views');610// =&gt; 200611</code></pre>612613<h3 id="app-enable-name-">app.enable(name)</h3>614615<p>Enable the given setting <em>name</em>:</p>616617<pre><code>app.enable('some arbitrary setting');618app.set('some arbitrary setting');619// =&gt; true620</code></pre>621622<h3 id="app-disable-name-">app.disable(name)</h3>623624<p>Disable the given setting <em>name</em>:</p>625626<pre><code>app.disable('some setting');627app.set('some setting');628// =&gt; false629</code></pre>630631<h3 id="app-configure-env-function-function-">app.configure(env|function[, function])</h3>632633<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>634635<pre><code>app.configure(function(){636 // executed for each env637});638639app.configure('development', function(){640 // executed for 'development' only641});642</code></pre>643644<h3 id="app-redirect-name-val-">app.redirect(name, val)</h3>

More docsTj Holowaychukbefore adoptionbedff32

lines 645–667why, one step back
645
646<p>For use with <code>res.redirect()</code> we can map redirects at the application level as shown below:</p>647648<pre><code>app.redirect('google', 'http://google.com');649</code></pre>650651<p>Now in a route we may call:</p>652653<p> res.redirect('google');</p>654655<p>We may also map dynamic redirects:</p>656657<pre><code>app.redirect('comments', function(req, res, params){658 return '/post/' + params.id + '/comments';659});660</code></pre>661662<p>So now we may do the following, and the redirect will dynamically adjust to663the context of the request. If we called this route with <em>GET /post/12</em> our664redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>665666<pre><code>app.get('/post/:id', function(req, res){667 res.redirect('comments');

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 668–676why, one step back
668});
669</code></pre>670671<h3 id="app-error-function-">app.error(function)</h3>672673<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter:</p>674675<pre><code>app.error(function(err, req, res, next){676 res.send(err.message, 500);

More docsTj Holowaychukbefore adoptionbedff32

line 677why, one step back
677});

Added configuration sectionTj Holowaychukbefore adoption83d2555

line 678why, one step back
678</code></pre>

Configuration docsTj Holowaychukbefore adoptioncb04364

line 679why, one step back
679

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 680–684why, one step back
680</div>
681 </div>682 </div>683 </body>684</html>