Skip to content
Myr

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

1 file+47 added−8 removed

modified:docs/guide.html+47 added-8 removed
338 unmodified lines
339<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>346347348<h3 id="routing">Routing</h3>
7 unmodified lines
356 res.send('user ' + req.params.id);357});358</code></pre>359358<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 example361when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>362363<pre><code>\/user\/([^\/]+)\/?364</code></pre>
67 unmodified lines
432433app.listen(3000);434</code></pre>435434<p>Typically we may use a &ldquo;dumb&rdquo; placeholder such as &ldquo;/user/:id&rdquo; which has no restrictions, however say for example we are limiting a user id to digits, we may use <em>&lsquo;/user/:id(\d+)&rsquo;</em> which will <em>not</em> match unless the placeholder value contains only digits.</p>436<p>Typically we may use a &ldquo;dumb&rdquo; placeholder such as &ldquo;/user/:id&rdquo; which has no restrictions, however say for example we are limiting a user id to digits, we may use <em>&lsquo;/user/:id([0-9]+)&rsquo;</em> which will <em>not</em> match unless the placeholder value contains only digits.</p>437438<h3 id="passing-route control">Passing Route Control</h3>439440<p>We may pass control to the next <em>matching</em> route, by calling the <em>third</em> argument,
79 unmodified lines
520<pre><code>app.use(express.logger());521app.use(express.bodyParser());522</code></pre>523524<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&rsquo;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>525526<p>Many Express applications may contain the line <em>app.use(app.router)</em>, while this may appear strange, it&rsquo;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>527528<pre><code>app.use(express.logger(...));529app.use(express.bodyParser(...));530app.use(express.cookieParser(...));531app.use(express.session(...));532app.use(app.router);533app.use(express.static(...));534app.use(express.errorHandler(...));535</code></pre>536537<p>First we add <em>logger()</em> so that it may wrap node&rsquo;s <em>req.end()</em> method, providing us with response-time data. Next the request&rsquo;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>538539<pre><code>var downloads = {};540541app.use(app.router);542app.use(express.static(__dirname + '/public'));543544app.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>551552<h3 id="route-middleware">Route Middleware</h3>553554<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 lines
624</code></pre>625626<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>627628<p>There are times when we may want to &ldquo;skip&rdquo; passed remaining route middleware, but continue matching subsequent routes. To do this we invoke <code>next()</code> with the string &ldquo;route&rdquo; <code>next('route')</code>. If no remaining routes match the request url then Express will respond with 404 Not Found.</p>629630<h3 id="http-methods">HTTP Methods</h3>631632<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 lines
1186 // handle1187});1188</code></pre>11891158<h3 id="res.download()">res.download(file[, filename[, callback]])</h3>1190<h3 id="res.download()">res.download(file[, filename[, callback[, callback2]]])</h3>11911192<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>11931194<pre><code>res.download('path/to/image.png');
5 unmodified lines
1200<pre><code>res.attachment(file);1201res.sendfile(file);1202</code></pre>12031172<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>12051206<pre><code>res.download(path, 'expenses.doc', function(err){1207 // handle1208});1209</code></pre>12101211<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>12121213<pre><code>res.download(path, function(err){1214 // error or finished1215}, function(err){1216 // connection related error1217});1218</code></pre>12191220<h3 id="res.send()">res.send(body|status[, headers|status[, status]])</h3>12211222<p>The <em>res.send()</em> method is a high level response utility allowing you to pass1223objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:</p>
49 unmodified lines
1273 // use req.cookies.rememberme1274});1275</code></pre>12761236<h3 id="res.clearcookie()">res.clearCookie(name)</h3>1277<h3 id="res.clearcookie()">res.clearCookie(name[, options])</h3>12781279<p>Clear cookie <em>name</em> by setting &ldquo;expires&rdquo; far in the past.</p>12801281<pre><code>res.clearCookie('rememberme');
283 unmodified lines
1565<p>Express also provides a few locals by default:</p>15661567<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>15711572<p>This method is aliased as <em>app.locals()</em>.</p>1573
203 unmodified lines