Skip to content
Myr

guide.html

This file

Path

docs/guide.html

771 lines · 23.1 KiB · HTML

Last changed by

Updated install docsTj Holowaychukbefore adoption193c4a9

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>

Added more view docsTj Holowaychukbefore adoption2f96506

lines 177–179why, one step back
177<li><a href="#View-Rendering">View Rendering</a></li>
178<li><a href="#View-Partials">View Partials</a></li>179<li><a href="#Template-Engines">Template Engines</a></li>

More docsTj Holowaychukbefore adoptionde81ee7

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

Error handling docsTj Holowaychukbefore adoption9fbb9fa

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

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

line 198why, one step back
198<li><a href="#app-listen-port-host-">app.listen()</a></li>

Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f

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

Started guideTj Holowaychukbefore adoption9e3a0c6

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

Added contrib docsTj Holowaychukbefore adoptionf0ba57b

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

Started guideTj Holowaychukbefore adoption9e3a0c6

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

Install docsTj Holowaychukbefore adoption63d02f1

lines 212–215why, one step back
212<h3 id="Installation">Installation</h3>
213214<p>npm:</p>215

Updated install docsTj Holowaychukbefore adoption193c4a9

lines 216–217why, one step back
216<pre><code>$ npm install connect
217$ npm install express

Install docsTj Holowaychukbefore adoption63d02f1

lines 218–221why, one step back
218</code></pre>
219220<p>curl (or similar):</p>221

Updated install docsTj Holowaychukbefore adoption193c4a9

line 222why, one step back
222<pre><code>$ curl -# http://github.com/visionmedia/express/raw/master/install.sh | sh

Install docsTj Holowaychukbefore adoption63d02f1

lines 223–224why, one step back
223</code></pre>
224

Updated install docsTj Holowaychukbefore adoption193c4a9

line 225why, one step back
225<p>git clone, first update the submodules:</p>

Install docsTj Holowaychukbefore adoption63d02f1

line 226why, one step back
226

Updated install docsTj Holowaychukbefore adoption193c4a9

lines 227–229why, one step back
227<pre><code>$ git submodule update --init
228$ make install229$ make install-support

Install docsTj Holowaychukbefore adoption63d02f1

lines 230–231why, one step back
230</code></pre>
231

Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2

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

Added configuration sectionTj Holowaychukbefore adoption83d2555

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

Moved config docs upTj Holowaychukbefore adoption6e8413f

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

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 237–246why, one step back
237
238<pre><code>var app = require('express').createServer();239240app.get('/', function(req, res){241 res.send('hello world');242});243244app.listen(3000);245</code></pre>246

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 247–285why, one step back
247<h3 id="Configuration">Configuration</h3>
248249<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers250can use the <em>configure()</em> method to setup needs required by the current environment. When251<em>configure()</em> is called without an environment name it will be run in <em>every</em> environment252prior to the environment specific callback.</p>253254<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces255in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyDecoder</em>.</p>256257<pre><code>app.configure(function(){258 app.use('/', connect.methodOverride());259 app.use('/', connect.bodyDecoder());260});261262app.configure('development', function(){263 app.use('/', connect.errorHandler({ dumpExceptions: true, showStack: true }));264});265266app.configure('production', function(){267 app.use('/', connect.errorHandler());268});269</code></pre>270271<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>272273<pre><code>app.configure(function(){274 app.set('views', __dirname + '/views');275 app.set('views');276 // =&gt; "... views directory ..."277278 app.enable('some feature');279 // same as app.set('some feature', true);280281 app.disable('some feature');282 // same as app.set('some feature', false);283});284</code></pre>285

More docsTj Holowaychukbefore adoptionde81ee7

lines 286–291why, one step back
286<p>To alter the environment we can set the <em>CONNECT_ENV</em> environment variable,
287or more specifically <em>EXPRESS_ENV</em>, for example:</p>288289<pre><code>$ EXPRESS_ENV=production node app.js290</code></pre>291

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 292–296why, one step back
292<h3 id="Settings">Settings</h3>
293294<p>Express supports the following settings out of the box:</p>295296<ul>

More docsTj Holowaychukbefore adoptionde81ee7

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

Install docsTj Holowaychukbefore adoption63d02f1

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

Moved config docs upTj Holowaychukbefore adoption6e8413f

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

Added more setting docsTj Holowaychukbefore adoption38281e6

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

Fixed reload views exampleTj Holowaychukbefore adoption5fcf971

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

Moved config docs upTj Holowaychukbefore adoption6e8413f

lines 303–305why, one step back
303</ul>
304305

Started guideTj Holowaychukbefore adoption9e3a0c6

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

Route passing docsTj Holowaychukbefore adoption0ace97c

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

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 312–358why, one step back
312
313<pre><code>app.get('/user/:id', function(req, res, params){314 res.send('user ' + params.id);315});316</code></pre>317318<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example319when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>320321<pre><code>\/user\/([^\/]+)\/?322</code></pre>323324<p>Literal regular expressions may also be passed for complex uses:</p>325326<pre><code>app.get(/^\/foo(bar)?$/, function(){});327</code></pre>328329<p>Below are some route examples, and the associated paths that they330may consume:</p>331332<pre><code> "/user/:id"333 /user/12334335 "/users/:id?"336 /users/5337 /users338339 "/files/*"340 /files/jquery.js341 /files/javascripts/jquery.js342343 "/file/*.*"344 /files/jquery.js345 /files/javascripts/jquery.js346347 "/user/:id/:operation?"348 /user/1349 /user/1/edit350351 "/products.:format"352 /products.json353 /products.xml354355 "/products.:format?"356 /products.json357 /products.xml358 /products

Route passing docsTj Holowaychukbefore adoption0ace97c

lines 359–375why, one step back
359</code></pre>
360361<h3 id="Passing-Route-Control">Passing Route Control</h3>362363<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,364the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>365366<pre><code>app.get('/users/:id?', function(req, res, params){367 if (params.id) {368 // do something369 } else {370 next();371 }372});373374app.get('/users', function(req, res, params){375 // do something else

Added configuration sectionTj Holowaychukbefore adoption83d2555

lines 376–390why, one step back
376});
377</code></pre>378379<h3 id="Middleware">Middleware</h3>380381<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/extjs/Connect">Connect</a> can be382passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>383384<pre><code>var connect = require('connect'),385 express = require('express');386387var app = express.createServer(388 connect.logger(),389 connect.bodyDecoder()390);

More docsTj Holowaychukbefore adoptionbedff32

lines 391–392why, one step back
391</code></pre>
392

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 393–444why, one step back
393<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks:</p>
394395<pre><code>app.use('/', connect.logger({ format: ':method :uri' }));396</code></pre>397398<h3 id="Error-Handling">Error Handling</h3>399400<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,401or passed to <em>next(err)</em>. Below is an example which serves different pages based on our402ad-hoc <em>NotFound</em> exception:</p>403404<pre><code>function NotFound(msg){405 this.name = 'NotFound';406 Error.call(this, msg);407 Error.captureStackTrace(this, arguments.callee);408}409410sys.inherits(NotFound, Error);411412app.get('/404', function(req, res){413 throw new NotFound;414});415416app.get('/500', function(req, res){417 throw new Error('keyboard cat!');418});419</code></pre>420421<p>We can call <em>app.error()</em> several times as shown below.422Here we check for an instanceof <em>NotFound</em> and show the423404 page, or we pass on to the next error handler.</p>424425<pre><code>app.error(function(err, req, res, next){426 if (err instanceof NotFound) {427 res.render('404.jade');428 } else {429 next(err);430 }431});432</code></pre>433434<p>Here we assume all errors as 500 for the simplicity of435this demo, however you can choose whatever you like</p>436437<pre><code>app.error(function(err, req, res){438 res.render('500.jade', {439 locals: {440 error: err441 } 442 });443});444</code></pre>

Added more view docsTj Holowaychukbefore adoption2f96506

lines 445–495why, one step back
445
446<h3 id="View-Rendering">View Rendering</h3>447448<p>View filenames take the form <em>Express</em>.<em>ENGINE</em>, where <em>ENGINE</em> is the name449of the module that will be required. For example the view <em>layout.ejs</em> will450tell the view system to <em>require('ejs')</em>, the module being loaded must (currently)451export the method <em>exports.render(str, options)</em> to comply with Express, however452with will likely be extensible in the future.</p>453454<p>Below is an example using <a href="http://github.com/visionmedia/haml.js">Haml.js</a> to render <em>index.html</em>,455and since we do not use <em>layout: false</em> the rendered contents of <em>index.html</em> will be passed as456the <em>body</em> local variable in <em>layout.haml</em>.</p>457458<pre><code>app.get('/', function(req, res){459 res.render('index.haml', {460 locals: { title: 'My Site' }461 });462});463</code></pre>464465<h3 id="View-Partials">View Partials</h3>466467<p>The Express view system has built-in support for partials and collections, which are468sort of "mini" views representing a document fragment. For example rather than iterating469in a view to display comments, we would use a partial with collection support:</p>470471<pre><code>partial('comment.haml', { collection: comments });472</code></pre>473474<p>To make things even less verbose we can assume the extension as <em>.haml</em> when omitted,475however if we wished we could use an ejs partial, within a haml view for example.</p>476477<pre><code>partial('comment', { collection: comments });478</code></pre>479480<p>And once again even further, when rendering a collection we can simply pass481an array, if no other options are desired:</p>482483<pre><code>partial('comments', comments);484</code></pre>485486<h3 id="Template-Engines">Template Engines</h3>487488<p>Below are a few template engines commonly used with Express:</p>489490<ul>491<li><a href="http://github.com/visionmedia/jade">Jade</a> haml.js successor</li>492<li><a href="http://github.com/visionmedia/haml.js">Haml</a> indented templates</li>493<li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>494</ul>495

Error handling docsTj Holowaychukbefore adoption9fbb9fa

line 496why, one step back
496

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 498–505why, one step back
498
499<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>500501<pre><code>req.header('Host');502req.header('host');503req.header('Accept', '*/*');504</code></pre>505

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 507–530why, one step back
507
508<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>509510<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise511the given <em>type</em> is matched by an exact match, and then subtypes. You512may pass the subtype such as "html" which is then converted internally513to "text/html" using the mime lookup table.</p>514515<pre><code>// Accept: text/html516req.accepts('html');517// =&gt; true518519// Accept: text/*; application/json520req.accepts('html');521req.accepts('text/html');522req.accepts('text/plain');523req.accepts('application/json');524// =&gt; true525526req.accepts('image/png');527req.accepts('png');528// =&gt; false529</code></pre>530

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 532–545why, one step back
532
533<p>Return the value of param <em>name</em> when present.</p>534535<ul>536<li>Checks route placeholders, ex: /user/:id</li>537<li>Checks query string params, ex: ?id=12</li>538<li>Checks urlencoded body params, ex: id=12</li>539</ul>540541542<p>To utilize urlencoded request bodies, <em>req.body</em>543should be an object. This can be done by using544the <em>connect.bodyDecoder</em> middleware.</p>545

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 547–564why, one step back
547
548<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>549550<pre><code>req.flash('info', 'email sent');551req.flash('error', 'email delivery failed');552req.flash('info', 'email re-sent');553// =&gt; 2554555req.flash('info');556// =&gt; ['email sent', 'email re-sent']557558req.flash('info');559// =&gt; []560561req.flash();562// =&gt; { error: ['email delivery failed'], info: [] }563</code></pre>564

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptiondfecbe5

lines 566–573why, one step back
566
567<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header568to see if it was issued by an <em>XMLHttpRequest</em>:</p>569570<pre><code>req.xhr571req.isXMLHttpRequest572</code></pre>573

More docsTj Holowaychukbefore adoptionde81ee7

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

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 575–587why, one step back
575
576<p>Get or set the response header <em>key</em>.</p>577578<pre><code>res.header('Content-Length');579// =&gt; undefined580581res.header('Content-Length', 123);582// =&gt; 123583584res.header('Content-Length');585// =&gt; 123586</code></pre>587

More docsTj Holowaychukbefore adoptionde81ee7

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

More docs :)Tj Holowaychukbefore adoption0c5fe61

lines 589–596why, one step back
589
590<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>591592<pre><code> var filename = 'path/to/image.png';593 res.contentType(filename);594 // res.headers['Content-Type'] is now "image/png"595</code></pre>596

More docsTj Holowaychukbefore adoptionde81ee7

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

Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1

lines 598–603why, one step back
598
599<p>Sets the <em>Content-Disposition</em> response header to "attachment", with optional <em>filename</em>.</p>600601<pre><code> res.attachment('path/to/my/image.png');602</code></pre>603

More docsTj Holowaychukbefore adoptionde81ee7

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

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

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

More docsTj Holowaychukbefore adoptionde81ee7

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

Docs for new res methodsTj Holowaychukbefore adoptionb08dab9

lines 615–627why, one step back
615
616<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>617618<pre><code>res.download('path/to/image.png');619res.download('path/to/image.png', 'foo.png');620</code></pre>621622<p>This is equivalent to:</p>623624<pre><code>res.attachment(file);625res.sendfile(file);626</code></pre>627

More docsTj Holowaychukbefore adoptionde81ee7

line 628why, one step back
628<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 629–645why, one step back
629
630<p>The <code>res.send()</code> method is a high level response utility allowing you to pass631objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status632code based responses. The following are all valid uses:</p>633634<pre><code> res.send(new Buffer('wahoo'));635 res.send({ some: 'json' });636 res.send('&lt;p&gt;some html&lt;/p&gt;');637 res.send('Sorry, cant find that', 404);638 res.send('text', { 'Content-Type': 'text/plain' }, 201);639 res.send(404);640</code></pre>641642<p>By default the <em>Content-Type</em> response header is set, however if explicitly643assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>644it will not be set again.</p>645

More docsTj Holowaychukbefore adoptionde81ee7

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

More docsTj Holowaychukbefore adoptionbedff32

lines 647–660why, one step back
647
648<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>649650<pre><code>res.redirect('/', 301);651res.redirect('/account');652res.redirect('http://google.com');653res.redirect('home');654res.redirect('back');655</code></pre>656657<p>Express supports "redirect mapping", which by default provides <em>home</em>, and <em>back</em>.658The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizes659the "home" setting and defaults to "/".</p>660

More docsTj Holowaychukbefore adoptionde81ee7

lines 661–702why, one step back
661<h3 id="app-set-name-val-">app.set(name[, val])</h3>
662663<p>Apply an application level setting <em>name</em> to <em>val</em>, or664get the value of <em>name</em> when <em>val</em> is not present:</p>665666<pre><code>app.set('reload views', 200);667app.set('reload views');668// =&gt; 200669</code></pre>670671<h3 id="app-enable-name-">app.enable(name)</h3>672673<p>Enable the given setting <em>name</em>:</p>674675<pre><code>app.enable('some arbitrary setting');676app.set('some arbitrary setting');677// =&gt; true678</code></pre>679680<h3 id="app-disable-name-">app.disable(name)</h3>681682<p>Disable the given setting <em>name</em>:</p>683684<pre><code>app.disable('some setting');685app.set('some setting');686// =&gt; false687</code></pre>688689<h3 id="app-configure-env-function-function-">app.configure(env|function[, function])</h3>690691<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>692693<pre><code>app.configure(function(){694 // executed for each env695});696697app.configure('development', function(){698 // executed for 'development' only699});700</code></pre>701702<h3 id="app-redirect-name-val-">app.redirect(name, val)</h3>

More docsTj Holowaychukbefore adoptionbedff32

lines 703–725why, one step back
703
704<p>For use with <code>res.redirect()</code> we can map redirects at the application level as shown below:</p>705706<pre><code>app.redirect('google', 'http://google.com');707</code></pre>708709<p>Now in a route we may call:</p>710711<p> res.redirect('google');</p>712713<p>We may also map dynamic redirects:</p>714715<pre><code>app.redirect('comments', function(req, res, params){716 return '/post/' + params.id + '/comments';717});718</code></pre>719720<p>So now we may do the following, and the redirect will dynamically adjust to721the context of the request. If we called this route with <em>GET /post/12</em> our722redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>723724<pre><code>app.get('/post/:id', function(req, res){725 res.redirect('comments');

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 726–730why, one step back
726});
727</code></pre>728729<h3 id="app-error-function-">app.error(function)</h3>730

More error docsTj Holowaychukbefore adoption6227f5c

lines 731–733why, one step back
731<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter as shown below.
732Note that we may set several error handlers by making several calls to this method, however the handler733should call <em>next(err)</em> if it does not wish to deal with the exception:</p>

Error handling docsTj Holowaychukbefore adoption9fbb9fa

lines 734–736why, one step back
734
735<pre><code>app.error(function(err, req, res, next){736 res.send(err.message, 500);

More docsTj Holowaychukbefore adoptionbedff32

line 737why, one step back
737});

Server#listen() docsTj Holowaychukbefore adoptionf3803c4

lines 738–747why, one step back
738</code></pre>
739740<h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>741742<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all743connections will be accepted via <em>INADDR_ANY</em>.</p>744745<pre><code>app.listen();746app.listen(3000);747app.listen(3000, 'n.n.n.n');

Added configuration sectionTj Holowaychukbefore adoption83d2555

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

Configuration docsTj Holowaychukbefore adoptioncb04364

line 749why, one step back
749

Unix domain socket docsTj Holowaychukbefore adoption2fa7907

lines 750–766why, one step back
750<p>The <em>port</em> argument may also be a string representing the path to a unix domain socket:</p>
751752<pre><code>app.listen('/tmp/express.sock');753</code></pre>754755<p>Then try it out:</p>756757<pre><code>$ telnet /tmp/express.sock758GET / HTTP/1.1759760HTTP/1.1 200 OK761Content-Type: text/plain762Content-Length: 11763764Hello World765</code></pre>766

Started guideTj Holowaychukbefore adoption9e3a0c6

lines 767–771why, one step back
767</div>
768 </div>769 </div>770 </body>771</html>