guide.html
This file
Path
docs/guide.html
768 lines · 23.0 KiB · HTML
Last changed by
Added more view docsTj Holowaychukbefore adoption2f96506
History of this fileTake it
Started guideTj Holowaychukbefore adoption9e3a0c6
lines 1–46why, one step back1<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 back47 text-decoration: none;Started guideTj Holowaychukbefore adoption9e3a0c6
lines 48–115why, one step back48 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; }115Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f
lines 116–126why, one step back116 #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 back127 padding: 1px 0;Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f
lines 128–132why, one step back128 list-style: none;129 }130 #toc li a {131 font-size: 11px;132 }Started guideTj Holowaychukbefore adoption9e3a0c6
line 133why, one step back133 #menu {Added contrib docsTj Holowaychukbefore adoptionf0ba57b
line 134why, one step back134 margin-left: 65px;Started guideTj Holowaychukbefore adoption9e3a0c6
lines 135–149why, one step back135 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 back150 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;Started guideTj Holowaychukbefore adoption9e3a0c6
lines 151–167why, one step back151 -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 back168 <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 back176<li><a href="#Error-Handling">Error Handling</a></li>Added more view docsTj Holowaychukbefore adoption2f96506
lines 177–179why, one step back177<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 back180<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 back197<li><a href="#app-error-function-">app.error()</a></li>Server#listen() docsTj Holowaychukbefore adoptionf3803c4
line 198why, one step back198<li><a href="#app-listen-port-host-">app.listen()</a></li>Merge branch 'docs' into connectTj Holowaychukbefore adoption492f70f
line 199why, one step back199</ul>Started guideTj Holowaychukbefore adoption9e3a0c6
lines 200–207why, one step back200 <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 back208 <li><a href="contrib.html">Contributing</a></li>Started guideTj Holowaychukbefore adoption9e3a0c6
lines 209–211why, one step back209 <li><a href="migrate.html">1.x Migration</a></li>210 </ul>211<div class='mp'>Install docsTj Holowaychukbefore adoption63d02f1
lines 212–228why, one step back212<h3 id="Installation">Installation</h3>213214<p>npm:</p>215216<pre><code>$ npm install express217</code></pre>218219<p>curl (or similar):</p>220221<pre><code>$ curl -# http://github.com/extjs/Connect/raw/master/install.sh | sh222</code></pre>223224<p>git clone or tarball for <strong>HEAD</strong>:</p>225226<pre><code>$ make install227</code></pre>228Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
line 229why, one step back229<h3 id="Creating-An-Application">Creating An Application</h3>Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 230–231why, one step back230231<p>The <em>express.Server</em> now inherits from <em>http.Server</em>, howeverMoved config docs upTj Holowaychukbefore adoption6e8413f
lines 232–233why, one step back232follows the same idiom by providing <em>express.createServer()</em> as shown below. This means233that you can utilize Express server's transparently with other libraries.</p>Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 234–243why, one step back234235<pre><code>var app = require('express').createServer();236237app.get('/', function(req, res){238 res.send('hello world');239});240241app.listen(3000);242</code></pre>243Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 244–282why, one step back244<h3 id="Configuration">Configuration</h3>245246<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers247can use the <em>configure()</em> method to setup needs required by the current environment. When248<em>configure()</em> is called without an environment name it will be run in <em>every</em> environment249prior to the environment specific callback.</p>250251<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces252in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyDecoder</em>.</p>253254<pre><code>app.configure(function(){255 app.use('/', connect.methodOverride());256 app.use('/', connect.bodyDecoder());257});258259app.configure('development', function(){260 app.use('/', connect.errorHandler({ dumpExceptions: true, showStack: true }));261});262263app.configure('production', function(){264 app.use('/', connect.errorHandler());265});266</code></pre>267268<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>269270<pre><code>app.configure(function(){271 app.set('views', __dirname + '/views');272 app.set('views');273 // => "... views directory ..."274275 app.enable('some feature');276 // same as app.set('some feature', true);277278 app.disable('some feature');279 // same as app.set('some feature', false);280});281</code></pre>282More docsTj Holowaychukbefore adoptionde81ee7
lines 283–288why, one step back283<p>To alter the environment we can set the <em>CONNECT_ENV</em> environment variable,284or more specifically <em>EXPRESS_ENV</em>, for example:</p>285286<pre><code>$ EXPRESS_ENV=production node app.js287</code></pre>288Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 289–293why, one step back289<h3 id="Settings">Settings</h3>290291<p>Express supports the following settings out of the box:</p>292293<ul>More docsTj Holowaychukbefore adoptionde81ee7
line 294why, one step back294<li><em>env</em> Application environment set internally, use <em>app.set('env')</em> to utilize</li>Install docsTj Holowaychukbefore adoption63d02f1
line 295why, one step back295<li><em>home</em> Application base path used with <em>res.redirect()</em></li>Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 296–297why, one step back296<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>297<li><em>view engine</em> Default view engine name for views rendered without extensions</li>Added more setting docsTj Holowaychukbefore adoption38281e6
line 298why, one step back298<li><em>reload views</em> Reloads altered views, by default watches for <em>mtime</em> changes withFixed reload views exampleTj Holowaychukbefore adoption5fcf971
line 299why, one step back299 with a 5 minute interval. Example: <em>app.set('reload views', 60000);</em></li>Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 300–302why, one step back300</ul>301302Started guideTj Holowaychukbefore adoption9e3a0c6
lines 303–306why, one step back303<h3 id="Routing">Routing</h3>304305<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.306For example we may want to render a user's account for the path <em>/user/12</em>, thisRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 307–308why, one step back307can be done by defining the route below. The values associated to the named placeholders,308are passed as the <em>third</em> argument, which here we name <em>params</em>.</p>Started guideTj Holowaychukbefore adoption9e3a0c6
lines 309–355why, one step back309310<pre><code>app.get('/user/:id', function(req, res, params){311 res.send('user ' + params.id);312});313</code></pre>314315<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example316when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>317318<pre><code>\/user\/([^\/]+)\/?319</code></pre>320321<p>Literal regular expressions may also be passed for complex uses:</p>322323<pre><code>app.get(/^\/foo(bar)?$/, function(){});324</code></pre>325326<p>Below are some route examples, and the associated paths that they327may consume:</p>328329<pre><code> "/user/:id"330 /user/12331332 "/users/:id?"333 /users/5334 /users335336 "/files/*"337 /files/jquery.js338 /files/javascripts/jquery.js339340 "/file/*.*"341 /files/jquery.js342 /files/javascripts/jquery.js343344 "/user/:id/:operation?"345 /user/1346 /user/1/edit347348 "/products.:format"349 /products.json350 /products.xml351352 "/products.:format?"353 /products.json354 /products.xml355 /productsRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 356–372why, one step back356</code></pre>357358<h3 id="Passing-Route-Control">Passing Route Control</h3>359360<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,361the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>362363<pre><code>app.get('/users/:id?', function(req, res, params){364 if (params.id) {365 // do something366 } else {367 next();368 }369});370371app.get('/users', function(req, res, params){372 // do something elseAdded configuration sectionTj Holowaychukbefore adoption83d2555
lines 373–387why, one step back373});374</code></pre>375376<h3 id="Middleware">Middleware</h3>377378<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/extjs/Connect">Connect</a> can be379passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>380381<pre><code>var connect = require('connect'),382 express = require('express');383384var app = express.createServer(385 connect.logger(),386 connect.bodyDecoder()387);More docsTj Holowaychukbefore adoptionbedff32
lines 388–389why, one step back388</code></pre>389Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 390–441why, one step back390<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks:</p>391392<pre><code>app.use('/', connect.logger({ format: ':method :uri' }));393</code></pre>394395<h3 id="Error-Handling">Error Handling</h3>396397<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,398or passed to <em>next(err)</em>. Below is an example which serves different pages based on our399ad-hoc <em>NotFound</em> exception:</p>400401<pre><code>function NotFound(msg){402 this.name = 'NotFound';403 Error.call(this, msg);404 Error.captureStackTrace(this, arguments.callee);405}406407sys.inherits(NotFound, Error);408409app.get('/404', function(req, res){410 throw new NotFound;411});412413app.get('/500', function(req, res){414 throw new Error('keyboard cat!');415});416</code></pre>417418<p>We can call <em>app.error()</em> several times as shown below.419Here we check for an instanceof <em>NotFound</em> and show the420404 page, or we pass on to the next error handler.</p>421422<pre><code>app.error(function(err, req, res, next){423 if (err instanceof NotFound) {424 res.render('404.jade');425 } else {426 next(err);427 }428});429</code></pre>430431<p>Here we assume all errors as 500 for the simplicity of432this demo, however you can choose whatever you like</p>433434<pre><code>app.error(function(err, req, res){435 res.render('500.jade', {436 locals: {437 error: err438 } 439 });440});441</code></pre>Added more view docsTj Holowaychukbefore adoption2f96506
lines 442–492why, one step back442443<h3 id="View-Rendering">View Rendering</h3>444445<p>View filenames take the form <em>Express</em>.<em>ENGINE</em>, where <em>ENGINE</em> is the name446of the module that will be required. For example the view <em>layout.ejs</em> will447tell the view system to <em>require('ejs')</em>, the module being loaded must (currently)448export the method <em>exports.render(str, options)</em> to comply with Express, however449with will likely be extensible in the future.</p>450451<p>Below is an example using <a href="http://github.com/visionmedia/haml.js">Haml.js</a> to render <em>index.html</em>,452and since we do not use <em>layout: false</em> the rendered contents of <em>index.html</em> will be passed as453the <em>body</em> local variable in <em>layout.haml</em>.</p>454455<pre><code>app.get('/', function(req, res){456 res.render('index.haml', {457 locals: { title: 'My Site' }458 });459});460</code></pre>461462<h3 id="View-Partials">View Partials</h3>463464<p>The Express view system has built-in support for partials and collections, which are465sort of "mini" views representing a document fragment. For example rather than iterating466in a view to display comments, we would use a partial with collection support:</p>467468<pre><code>partial('comment.haml', { collection: comments });469</code></pre>470471<p>To make things even less verbose we can assume the extension as <em>.haml</em> when omitted,472however if we wished we could use an ejs partial, within a haml view for example.</p>473474<pre><code>partial('comment', { collection: comments });475</code></pre>476477<p>And once again even further, when rendering a collection we can simply pass478an array, if no other options are desired:</p>479480<pre><code>partial('comments', comments);481</code></pre>482483<h3 id="Template-Engines">Template Engines</h3>484485<p>Below are a few template engines commonly used with Express:</p>486487<ul>488<li><a href="http://github.com/visionmedia/jade">Jade</a> haml.js successor</li>489<li><a href="http://github.com/visionmedia/haml.js">Haml</a> indented templates</li>490<li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>491</ul>492Error handling docsTj Holowaychukbefore adoption9fbb9fa
line 493why, one step back493More docsTj Holowaychukbefore adoptionde81ee7
line 494why, one step back494<h3 id="req-header-key-defaultValue-">req.header(key[, defaultValue])</h3>More docsTj Holowaychukbefore adoptiondfecbe5
lines 495–502why, one step back495496<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>497498<pre><code>req.header('Host');499req.header('host');500req.header('Accept', '*/*');501</code></pre>502More docsTj Holowaychukbefore adoptionde81ee7
line 503why, one step back503<h3 id="req-accepts-type-">req.accepts(type)</h3>More docsTj Holowaychukbefore adoptiondfecbe5
lines 504–527why, one step back504505<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>506507<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise508the given <em>type</em> is matched by an exact match, and then subtypes. You509may pass the subtype such as "html" which is then converted internally510to "text/html" using the mime lookup table.</p>511512<pre><code>// Accept: text/html513req.accepts('html');514// => true515516// Accept: text/*; application/json517req.accepts('html');518req.accepts('text/html');519req.accepts('text/plain');520req.accepts('application/json');521// => true522523req.accepts('image/png');524req.accepts('png');525// => false526</code></pre>527More docsTj Holowaychukbefore adoptionde81ee7
line 528why, one step back528<h3 id="req-param-name-">req.param(name)</h3>More docsTj Holowaychukbefore adoptiondfecbe5
lines 529–542why, one step back529530<p>Return the value of param <em>name</em> when present.</p>531532<ul>533<li>Checks route placeholders, ex: /user/:id</li>534<li>Checks query string params, ex: ?id=12</li>535<li>Checks urlencoded body params, ex: id=12</li>536</ul>537538539<p>To utilize urlencoded request bodies, <em>req.body</em>540should be an object. This can be done by using541the <em>connect.bodyDecoder</em> middleware.</p>542More docsTj Holowaychukbefore adoptionde81ee7
line 543why, one step back543<h3 id="req-flash-type-msg-">req.flash(type[, msg])</h3>More docsTj Holowaychukbefore adoptiondfecbe5
lines 544–561why, one step back544545<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>546547<pre><code>req.flash('info', 'email sent');548req.flash('error', 'email delivery failed');549req.flash('info', 'email re-sent');550// => 2551552req.flash('info');553// => ['email sent', 'email re-sent']554555req.flash('info');556// => []557558req.flash();559// => { error: ['email delivery failed'], info: [] }560</code></pre>561More docsTj Holowaychukbefore adoptionde81ee7
line 562why, one step back562<h3 id="req-isXMLHttpRequest">req.isXMLHttpRequest</h3>More docsTj Holowaychukbefore adoptiondfecbe5
lines 563–570why, one step back563564<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header565to see if it was issued by an <em>XMLHttpRequest</em>:</p>566567<pre><code>req.xhr568req.isXMLHttpRequest569</code></pre>570More docsTj Holowaychukbefore adoptionde81ee7
line 571why, one step back571<h3 id="res-header-key-val-">res.header(key[, val])</h3>More docs :)Tj Holowaychukbefore adoption0c5fe61
lines 572–584why, one step back572573<p>Get or set the response header <em>key</em>.</p>574575<pre><code>res.header('Content-Length');576// => undefined577578res.header('Content-Length', 123);579// => 123580581res.header('Content-Length');582// => 123583</code></pre>584More docsTj Holowaychukbefore adoptionde81ee7
line 585why, one step back585<h3 id="res-contentType-type-">res.contentType(type)</h3>More docs :)Tj Holowaychukbefore adoption0c5fe61
lines 586–593why, one step back586587<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>588589<pre><code> var filename = 'path/to/image.png';590 res.contentType(filename);591 // res.headers['Content-Type'] is now "image/png"592</code></pre>593More docsTj Holowaychukbefore adoptionde81ee7
line 594why, one step back594<h3 id="res-attachment-filename-">res.attachment([filename])</h3>Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1
lines 595–600why, one step back595596<p>Sets the <em>Content-Disposition</em> response header to "attachment", with optional <em>filename</em>.</p>597598<pre><code> res.attachment('path/to/my/image.png');599</code></pre>600More docsTj Holowaychukbefore adoptionde81ee7
line 601why, one step back601<h3 id="res-sendfile-path-">res.sendfile(path)</h3>Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
lines 602–610why, one step back602603<p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>604605<pre><code>res.sendfile('path/to/my.file');606</code></pre>607608<p><strong>NOTE</strong>: this is <em>not</em> a replacement for Connect's <em>staticProvider</em> middleware,609nor does it perform any security checks, use with caution when using in a dynamic manor.</p>610More docsTj Holowaychukbefore adoptionde81ee7
line 611why, one step back611<h3 id="res-download-file-filename-">res.download(file[, filename])</h3>Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
lines 612–624why, one step back612613<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>614615<pre><code>res.download('path/to/image.png');616res.download('path/to/image.png', 'foo.png');617</code></pre>618619<p>This is equivalent to:</p>620621<pre><code>res.attachment(file);622res.sendfile(file);623</code></pre>624More docsTj Holowaychukbefore adoptionde81ee7
line 625why, one step back625<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 626–642why, one step back626627<p>The <code>res.send()</code> method is a high level response utility allowing you to pass628objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status629code based responses. The following are all valid uses:</p>630631<pre><code> res.send(new Buffer('wahoo'));632 res.send({ some: 'json' });633 res.send('<p>some html</p>');634 res.send('Sorry, cant find that', 404);635 res.send('text', { 'Content-Type': 'text/plain' }, 201);636 res.send(404);637</code></pre>638639<p>By default the <em>Content-Type</em> response header is set, however if explicitly640assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>641it will not be set again.</p>642More docsTj Holowaychukbefore adoptionde81ee7
line 643why, one step back643<h3 id="res-redirect-url-status-">res.redirect(url[, status])</h3>More docsTj Holowaychukbefore adoptionbedff32
lines 644–657why, one step back644645<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>646647<pre><code>res.redirect('/', 301);648res.redirect('/account');649res.redirect('http://google.com');650res.redirect('home');651res.redirect('back');652</code></pre>653654<p>Express supports "redirect mapping", which by default provides <em>home</em>, and <em>back</em>.655The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizes656the "home" setting and defaults to "/".</p>657More docsTj Holowaychukbefore adoptionde81ee7
lines 658–699why, one step back658<h3 id="app-set-name-val-">app.set(name[, val])</h3>659660<p>Apply an application level setting <em>name</em> to <em>val</em>, or661get the value of <em>name</em> when <em>val</em> is not present:</p>662663<pre><code>app.set('reload views', 200);664app.set('reload views');665// => 200666</code></pre>667668<h3 id="app-enable-name-">app.enable(name)</h3>669670<p>Enable the given setting <em>name</em>:</p>671672<pre><code>app.enable('some arbitrary setting');673app.set('some arbitrary setting');674// => true675</code></pre>676677<h3 id="app-disable-name-">app.disable(name)</h3>678679<p>Disable the given setting <em>name</em>:</p>680681<pre><code>app.disable('some setting');682app.set('some setting');683// => false684</code></pre>685686<h3 id="app-configure-env-function-function-">app.configure(env|function[, function])</h3>687688<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>689690<pre><code>app.configure(function(){691 // executed for each env692});693694app.configure('development', function(){695 // executed for 'development' only696});697</code></pre>698699<h3 id="app-redirect-name-val-">app.redirect(name, val)</h3>More docsTj Holowaychukbefore adoptionbedff32
lines 700–722why, one step back700701<p>For use with <code>res.redirect()</code> we can map redirects at the application level as shown below:</p>702703<pre><code>app.redirect('google', 'http://google.com');704</code></pre>705706<p>Now in a route we may call:</p>707708<p> res.redirect('google');</p>709710<p>We may also map dynamic redirects:</p>711712<pre><code>app.redirect('comments', function(req, res, params){713 return '/post/' + params.id + '/comments';714});715</code></pre>716717<p>So now we may do the following, and the redirect will dynamically adjust to718the context of the request. If we called this route with <em>GET /post/12</em> our719redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>720721<pre><code>app.get('/post/:id', function(req, res){722 res.redirect('comments');Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 723–727why, one step back723});724</code></pre>725726<h3 id="app-error-function-">app.error(function)</h3>727More error docsTj Holowaychukbefore adoption6227f5c
lines 728–730why, one step back728<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter as shown below.729Note that we may set several error handlers by making several calls to this method, however the handler730should call <em>next(err)</em> if it does not wish to deal with the exception:</p>Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 731–733why, one step back731732<pre><code>app.error(function(err, req, res, next){733 res.send(err.message, 500);More docsTj Holowaychukbefore adoptionbedff32
line 734why, one step back734});Server#listen() docsTj Holowaychukbefore adoptionf3803c4
lines 735–744why, one step back735</code></pre>736737<h3 id="app-listen-port-host-">app.listen([port[, host]])</h3>738739<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all740connections will be accepted via <em>INADDR_ANY</em>.</p>741742<pre><code>app.listen();743app.listen(3000);744app.listen(3000, 'n.n.n.n');Added configuration sectionTj Holowaychukbefore adoption83d2555
line 745why, one step back745</code></pre>Configuration docsTj Holowaychukbefore adoptioncb04364
line 746why, one step back746Unix domain socket docsTj Holowaychukbefore adoption2fa7907
lines 747–763why, one step back747<p>The <em>port</em> argument may also be a string representing the path to a unix domain socket:</p>748749<pre><code>app.listen('/tmp/express.sock');750</code></pre>751752<p>Then try it out:</p>753754<pre><code>$ telnet /tmp/express.sock755GET / HTTP/1.1756757HTTP/1.1 200 OK758Content-Type: text/plain759Content-Length: 11760761Hello World762</code></pre>763Started guideTj Holowaychukbefore adoption9e3a0c6
lines 764–768why, one step back764</div>765 </div>766 </div>767 </body>768</html>