docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 8d6f167 · Browse files · 1 file·+47 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+47 added−8 removed
- other:+47 added−8 removed
Provenance
- Commit
3588c1e- Tree
1f2cd18- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
8d6f167
1 file changed+47 added−8 removed
modified:docs/guide.html+47 added-8 removed
338 unmodified lines339 <li><em>home</em> Application base path used for <em>res.redirect()</em> and transparently handling mounted apps.</li>340 <li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>341 <li><em>view engine</em> Default view engine name for views rendered without extensions</li>342 <li><em>view options</em> An object specifying global view options</li>343+<li><em>view cache</em> Enable view caching (enabled in production)</li>344+<li><em>case sensitive routes</em> Enable case-sensitive routing</li>345 </ul>346 347 348 <h3 id="routing">Routing</h3>7 unmodified lines356 res.send('user ' + req.params.id);357 });358 </code></pre>359 358-<p>A route is simply a string which is compiled to a <em>RegExp</em> internally. For example360+<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example361 when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>362 363 <pre><code>\/user\/([^\/]+)\/?364 </code></pre>67 unmodified lines432 433 app.listen(3000);434 </code></pre>435 434-<p>Typically we may use a “dumb” placeholder such as “/user/:id” which has no restrictions, however say for example we are limiting a user id to digits, we may use <em>‘/user/:id(\d+)’</em> which will <em>not</em> match unless the placeholder value contains only digits.</p>436+<p>Typically we may use a “dumb” placeholder such as “/user/:id” which has no restrictions, however say for example we are limiting a user id to digits, we may use <em>‘/user/:id([0-9]+)’</em> which will <em>not</em> match unless the placeholder value contains only digits.</p>437 438 <h3 id="passing-route control">Passing Route Control</h3>439 440 <p>We may pass control to the next <em>matching</em> route, by calling the <em>third</em> argument,79 unmodified lines520 <pre><code>app.use(express.logger());521 app.use(express.bodyParser());522 </code></pre>523 524+<p>Middleware ordering is important, when Connect receives a request the <em>first</em> middleware we pass to <em>createServer()</em> or <em>use()</em> is executed with three parameters, <em>request</em>, <em>response</em>, and a callback function usually named <em>next</em>. When <em>next()</em> is invoked the second middleware will then have it’s turn and so on. This is important to note because many middleware depend on each other, for example <em>methodOverride()</em> checks <em>req.body.</em>method<em> for the HTTP method override, however </em>bodyParser()<em> parses the request body and populates </em>req.body<em>. Another example of this is cookie parsing and session support, we must first </em>use()<em> </em>cookieParser()<em> followed by </em>session()_.</p>525+526+<p>Many Express applications may contain the line <em>app.use(app.router)</em>, while this may appear strange, it’s simply the middleware function that contains all defined routes, and performs route lookup based on the current request url and HTTP method. Express allows you to position this middleware, though by default it will be added to the bottom. By positioning the router, we can alter middleware precedence, for example we may want to add error reporting as the <em>last</em> middleware so that any exception passed to <em>next()</em> will be handled by it, or perhaps we want static file serving to have low precedence, allowing our routes to intercept requests to a static file to count downloads etc. This may look a little like below</p>527+528+<pre><code>app.use(express.logger(...));529+app.use(express.bodyParser(...));530+app.use(express.cookieParser(...));531+app.use(express.session(...));532+app.use(app.router);533+app.use(express.static(...));534+app.use(express.errorHandler(...));535+</code></pre>536+537+<p>First we add <em>logger()</em> so that it may wrap node’s <em>req.end()</em> method, providing us with response-time data. Next the request’s body will be parsed (if any), followed by cookie parsing and session support, meaning <em>req.session</em> will be defined by the time we hit our routes in <em>app.router</em>. If a request such as <em>GET /javascripts/jquery.js</em> is handled by our routes, and we do not call <em>next()</em> then the <em>static()</em> middleware will never see this request, however if were to define a route as shown below, we can record stats, refuse downloads, consume download credits etc.</p>538+539+<pre><code>var downloads = {};540+541+app.use(app.router);542+app.use(express.static(__dirname + '/public'));543+544+app.get('/*', function(req, res, next){545+ var file = req.params[0];546+ downloads[file] = downloads[file] || 0;547+ downloads[file]++;548+ next();549+});550+</code></pre>551+552 <h3 id="route-middleware">Route Middleware</h3>553 554 <p>Routes may utilize route-specific middleware by passing one or more additional callbacks (or arrays) to the method. This feature is extremely useful for restricting access, loading data used by the route etc.</p>555 68 unmodified lines624 </code></pre>625 626 <p>For this example in full, view the <a href="http://github.com/visionmedia/express/blob/master/examples/route-middleware/app.js">route middleware example</a> in the repository.</p>627 628+<p>There are times when we may want to “skip” passed remaining route middleware, but continue matching subsequent routes. To do this we invoke <code>next()</code> with the string “route” <code>next('route')</code>. If no remaining routes match the request url then Express will respond with 404 Not Found.</p>629+630 <h3 id="http-methods">HTTP Methods</h3>631 632 <p>We have seen <em>app.get()</em> a few times, however Express also exposes other familiar HTTP verbs in the same manor, such as <em>app.post()</em>, <em>app.del()</em>, etc.</p>633 552 unmodified lines1186 // handle1187 });1188 </code></pre>1189 1158-<h3 id="res.download()">res.download(file[, filename[, callback]])</h3>1190+<h3 id="res.download()">res.download(file[, filename[, callback[, callback2]]])</h3>1191 1192 <p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>1193 1194 <pre><code>res.download('path/to/image.png');5 unmodified lines1200 <pre><code>res.attachment(file);1201 res.sendfile(file);1202 </code></pre>1203 1172-<p>An optional callback may be supplied as either the second or third argument, which is passed to <em>res.sendfile()</em>:</p>1204+<p>An optional callback may be supplied as either the second or third argument, which is passed to <em>res.sendfile()</em>. Within this callback you may still respond, as the header has not been sent.</p>1205 1206 <pre><code>res.download(path, 'expenses.doc', function(err){1207 // handle1208 });1209 </code></pre>1210 1211+<p>An optional second callback, <em>callback2</em> may be given to allow you to act on connection related errors, however you should not attempt to respond.</p>1212+1213+<pre><code>res.download(path, function(err){1214+ // error or finished1215+}, function(err){1216+ // connection related error1217+});1218+</code></pre>1219+1220 <h3 id="res.send()">res.send(body|status[, headers|status[, status]])</h3>1221 1222 <p>The <em>res.send()</em> method is a high level response utility allowing you to pass1223 objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:</p>49 unmodified lines1273 // use req.cookies.rememberme1274 });1275 </code></pre>1276 1236-<h3 id="res.clearcookie()">res.clearCookie(name)</h3>1277+<h3 id="res.clearcookie()">res.clearCookie(name[, options])</h3>1278 1279 <p>Clear cookie <em>name</em> by setting “expires” far in the past.</p>1280 1281 <pre><code>res.clearCookie('rememberme');283 unmodified lines1565 <p>Express also provides a few locals by default:</p>1566 1567 <pre><code>- `settings` the app's settings object1568 - `filename` the view's filename1528-- `request` the request object1529-- `response` the response object1530-- `app` the application itself1569+- `layout(path)` specify the layout from within a view1570 </code></pre>1571 1572 <p>This method is aliased as <em>app.locals()</em>.</p>1573 203 unmodified lines