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 50006f7 · Browse files · 3 files·+104 added−106 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+104 added−106 removed
- other:+104 added−106 removed
Provenance
- Commit
14bd50e- Tree
4fdb53f- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
50006f7
3 files changed+104 added−106 removed
modified:docs/guide.1+32 added-37 removed
5 unmodified lines6 .SH "NAME"7 \fBguide\fR8 .9 .SS "Installation"10-curl:10 .12-.IP "" 413-.11 .nf12 16-$ curl \-# http://expressjs\.com/install\.sh | sh13+$ npm install express14 .15 .fi16 .20-.IP "" 017+.SS "Creating A Server"18+To create an instance of the \fIexpress\.HTTPServer\fR, simply invoke the \fIcreateServer()\fR method\. With our instance \fIapp\fR we can then define routes based on the HTTP verbs, in this example \fIapp\.get()\fR\.19 .22-.P23-npm:24-.20 .IP "" 421 .22 .nf23 29-$ npm install express24+var app = require(\'express\')\.createServer();25+26+app\.get(\'/\', function(req, res){27+ res\.send(\'hello world\');28+});29+30+app\.listen(3000);31 .32 .fi33 .34 .IP "" 035 .35-.SS "Creating An Application"36-The \fIexpress\.Server\fR now inherits from \fIhttp\.Server\fR, however follows the same idiom by providing \fIexpress\.createServer()\fR as shown below\. This means that you can utilize Express server\'s transparently with other libraries\.36+.SS "Creating An HTTPS Server"37+To initialize a \fIexpress\.HTTPSServer\fR we do the same as above, however we pass an options object, accepting \fIkey\fR, \fIcert\fR and the others mentioned in node\'s https documentation \fIhttp://nodejs\.org/docs/v0\.3\.7/api/https\.html#https\.createServer\fR\.38 .39 .IP "" 440 .41 .nf42 42-var app = require(\'express\')\.createServer();43-44-app\.get(\'/\', function(req, res){45- res\.send(\'hello world\');46-});47-48-app\.listen(3000);43+ var app = require(\'express\')\.createServer({ key: \.\.\. });44 .45 .fi46 .47 .IP "" 01 unmodified line49 .SS "Configuration"50 Express supports arbitrary environments, such as \fIproduction\fR and \fIdevelopment\fR\. Developers can use the \fIconfigure()\fR method to setup needs required by the current environment\. When \fIconfigure()\fR is called without an environment name it will be run in \fIevery\fR environment prior to the environment specific callback\.51 .52 .P58-In the example below we only \fIdumpExceptions\fR, and respond with exception stack traces in \fIdevelopment\fR mode, however for both environments we utilize \fImethodOverride\fR and \fIbodyDecoder\fR\. Note the use of \fIapp\.router\fR, which can (optionally) be used to mount the application routes, otherwise the first call to \fIapp\.{get,put,del,post}()\fR will mount the routes\.53+In the example below we only \fIdumpExceptions\fR, and respond with exception stack traces in \fIdevelopment\fR mode, however for both environments we utilize \fImethodOverride\fR and \fIbodyParser\fR\. Note the use of \fIapp\.router\fR, which can (optionally) be used to mount the application routes, otherwise the first call to \fIapp\.get()\fR, \fIapp\.post()\fR, etc will mount the routes\.54 .55 .IP "" 456 .57 .nf58 59 app\.configure(function(){60 app\.use(express\.methodOverride());66- app\.use(express\.bodyDecoder());61+ app\.use(express\.bodyParser());62 app\.use(app\.router);63 });64 65 app\.configure(\'development\', function(){71- app\.use(express\.staticProvider(__dirname + \'/public\'));66+ app\.use(express\.static(__dirname + \'/public\'));67 app\.use(express\.errorHandler({ dumpExceptions: true, showStack: true }));68 });69 70 app\.configure(\'production\', function(){71 var oneYear = 31557600000;77- app\.use(express\.staticProvider({ root: __dirname + \'/public\', maxAge: oneYear }));78- app\.use(express\.errorHandler());72+ app\.use(express\.static({ root: __dirname + \'/public\', maxAge: oneYear }));73+ app\.use(express\.errorHandler());74 });75 .76 .fi77 .5 unmodified lines83 .IP "" 484 .85 .nf86 92-app\.configure(function(){87+ app\.configure(function(){88 app\.set(\'views\', __dirname + \'/views\');89 app\.set(\'views\');95- // => "\.\.\. views directory \.\.\."90+ // => "/absolute/path/to/views"91 92 app\.enable(\'some feature\');93 // same as app\.set(\'some feature\', true);94 95 app\.disable(\'some feature\');96 // same as app\.set(\'some feature\', false);97+98+ app\.enabled(\'some feature\')99+ // => false102-});100+ });101 .102 .fi103 .104 .IP "" 017 unmodified lines122 .SS "Settings"123 Express supports the following settings out of the box:124 .125 .IP "\(bu" 4128-\fIenv\fR Application environment set internally, use \fIapp\.set(\'env\')\fR on \fIServer#listen()\fR129-.130-.IP "\(bu" 4126 \fIhome\fR Application base path used for \fIres\.redirect()\fR and transparently handling mounted apps\.127 .128 .IP "\(bu" 4129 \fIviews\fR Root views directory defaulting to \fBCWD/views\fR112 unmodified lines242 .243 .IP "" 0244 .245 .P251-For example we can \fBPOST\fR some json, and echo the json back using the \fIbodyDecoder\fR middleware which will parse json request bodies (as well as others), and place the result in \fIreq\.body\fR:246+For example we can \fBPOST\fR some json, and echo the json back using the \fIbodyParser\fR middleware which will parse json request bodies (as well as others), and place the result in \fIreq\.body\fR:247 .248 .IP "" 4249 .250 .nf251 252 var express = require(\'express\')253 , app = express\.createServer();254 260-app\.use(express\.bodyDecoder());255+app\.use(express\.bodyParser());256 257 app\.post(\'/\', function(req, res){258 res\.send(req\.body);259 });4 unmodified lines264 .265 .IP "" 0266 .267 .P273-Express 2\.0\.0\-pre also supports named capture groups\. Typically we may use a "dump" placeholder such as "/user/:id" which has no restraints, however say for example we are limiting a user id to digits, we may use \fI\'/user/:id(\ed+)\'\fR which will \fInot\fR match unless the placeholder value contains only digits\.268+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 \fI\'/user/:id(\ed+)\'\fR which will \fInot\fR match unless the placeholder value contains only digits\.269 .270 .SS "Passing Route Control"276-We may pass control to the next \fImatching\fR route, by calling the \fIthird\fR argument, the \fInext()\fR function\. When a match cannot be made, control is passed back to Connect, and middleware continue to be invoked\. The same is true for several routes which have the same path defined, they will simply be executed in order until one does \fInot\fR call \fInext()\fR\.271+We may pass control to the next \fImatching\fR route, by calling the \fIthird\fR argument, the \fInext()\fR function\. When a match cannot be made, control is passed back to Connect, and middleware continue to be invoked in the order that they are added via \fIuse()\fR\. The same is true for several routes which have the same path defined, they will simply be executed in order until one does \fInot\fR call \fInext()\fR and decides to respond\.272 .273 .IP "" 4274 .275 .nf15 unmodified lines291 .292 .IP "" 0293 .294 .P300-Express 1\.0 also introduces the \fIall()\fR method, which provides a route callback matching any HTTP method\. This is useful in many ways, one example being the loading of resources before executing subsequent routes as shown below:295+The \fIapp\.all()\fR method is useful for applying the same logic for all HTTP verbs in a single call\. Below we use this to load a user from our fake database, and assign it to \fIreq\.user\fR\.296 .297 .IP "" 4298 .299 .nf43 unmodified lines343 344 var express = require(\'express\');345 346 var app = express\.createServer(352- express\.logger(),353- express\.bodyDecoder()347+ express\.logger()348+ , express\.bodyParser()349 );350 .351 .fi352 .353 .IP "" 0354 .355 .P361-Alternatively we can \fIuse()\fR them which is useful when adding middleware within \fIconfigure()\fR blocks:356+Alternatively we can \fIuse()\fR them which is useful when adding middleware within \fIconfigure()\fR blocks, in a progressive manor\.357 .358 .IP "" 4359 .360 .nf1606 unmodified linesmodified:docs/guide.html+32 added-30 removed
178 unmodified lines179 </a>180 <div id="wrapper">181 <div id="container"><ul id="toc">182 <li><a href="#Installation">Installation</a></li>183-<li><a href="#Creating-An-Application">Creating An Application</a></li>183+<li><a href="#Creating-A-Server">Creating A Server</a></li>184+<li><a href="#Creating-An-HTTPS-Server">Creating An HTTPS Server</a></li>185 <li><a href="#Configuration">Configuration</a></li>186 <li><a href="#Settings">Settings</a></li>187 <li><a href="#Routing">Routing</a></li>188 <li><a href="#Passing-Route-Control">Passing Route Control</a></li>60 unmodified lines249 <code>guide</code>250 </p>251 <h3 id="Installation">Installation</h3>252 252-<p>curl:</p>253-254-<pre><code>$ curl -# http://expressjs.com/install.sh | sh255-</code></pre>256-257-<p>npm:</p>258-253 <pre><code>$ npm install express254 </code></pre>255 262-<h3 id="Creating-An-Application">Creating An Application</h3>256+<h3 id="Creating-A-Server">Creating A Server</h3>257 264-<p>The <em>express.Server</em> now inherits from <em>http.Server</em>, however265-follows the same idiom by providing <em>express.createServer()</em> as shown below. This means266-that you can utilize Express server's transparently with other libraries.</p>258+<p> To create an instance of the <em>express.HTTPServer</em>, simply invoke the <em>createServer()</em> method. With our instance <em>app</em> we can then define routes based on the HTTP verbs, in this example <em>app.get()</em>.</p>259 260 <pre><code>var app = require('express').createServer();261 262 app.get('/', function(req, res){2 unmodified lines265 266 app.listen(3000);267 </code></pre>268 269+<h3 id="Creating-An-HTTPS-Server">Creating An HTTPS Server</h3>270+271+<p> To initialize a <em>express.HTTPSServer</em> we do the same as above, however we272+ pass an options object, accepting <em>key</em>, <em>cert</em> and the others mentioned in node's <a href="http://nodejs.org/docs/v0.3.7/api/https.html#https.createServer">https documentation</a>.</p>273+274+<pre><code> var app = require('express').createServer({ key: ... });275+</code></pre>276+277 <h3 id="Configuration">Configuration</h3>278 279 <p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers280 can use the <em>configure()</em> method to setup needs required by the current environment. When281 <em>configure()</em> is called without an environment name it will be run in <em>every</em> environment282 prior to the environment specific callback.</p>283 284 <p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces285-in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyDecoder</em>.285+in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyParser</em>.286 Note the use of <em>app.router</em>, which can (optionally) be used to mount the application routes,287-otherwise the first call to <em>app.{get,put,del,post}()</em> will mount the routes.</p>287+otherwise the first call to <em>app.get()</em>, <em>app.post()</em>, etc will mount the routes.</p>288 289 <pre><code>app.configure(function(){290 app.use(express.methodOverride());291- app.use(express.bodyDecoder());291+ app.use(express.bodyParser());292 app.use(app.router);293 });294 295 app.configure('development', function(){296- app.use(express.staticProvider(__dirname + '/public'));296+ app.use(express.static(__dirname + '/public'));297 app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));298 });299 300 app.configure('production', function(){301 var oneYear = 31557600000;302- app.use(express.staticProvider({ root: __dirname + '/public', maxAge: oneYear }));303- app.use(express.errorHandler());302+ app.use(express.static({ root: __dirname + '/public', maxAge: oneYear }));303+ app.use(express.errorHandler());304 });305 </code></pre>306 307 <p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>308 309-<pre><code>app.configure(function(){309+<pre><code> app.configure(function(){310 app.set('views', __dirname + '/views');311 app.set('views');312- // => "... views directory ..."312+ // => "/absolute/path/to/views"313 314 app.enable('some feature');315 // same as app.set('some feature', true);316 317 app.disable('some feature');318 // same as app.set('some feature', false);319+320+ app.enabled('some feature')321+ // => false319-});322+ });323 </code></pre>324 325 <p>To alter the environment we can set the <em>NODE_ENV</em> environment variable, for example:</p>326 6 unmodified lines333 334 <p>Express supports the following settings out of the box:</p>335 336 <ul>334-<li><em>env</em> Application environment set internally, use <em>app.set('env')</em> on <em>Server#listen()</em></li>337 <li><em>home</em> Application base path used for <em>res.redirect()</em> and transparently handling mounted apps.</li>338 <li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>339 <li><em>view engine</em> Default view engine name for views rendered without extensions</li>340 <li><em>view options</em> An object specifying global view options</li>73 unmodified lines414 /user/12415 /user/12.json416 </code></pre>417 416-<p>For example we can <strong>POST</strong> some json, and echo the json back using the <em>bodyDecoder</em> middleware which will parse json request bodies (as well as others), and place the result in <em>req.body</em>:</p>418+<p>For example we can <strong>POST</strong> some json, and echo the json back using the <em>bodyParser</em> middleware which will parse json request bodies (as well as others), and place the result in <em>req.body</em>:</p>419 420 <pre><code>var express = require('express')421 , app = express.createServer();422 421-app.use(express.bodyDecoder());423+app.use(express.bodyParser());424 425 app.post('/', function(req, res){426 res.send(req.body);427 });428 429 app.listen(3000);430 </code></pre>431 430-<p>Express 2.0.0-pre also supports named capture groups. Typically we may use a "dump" placeholder such as "/user/:id" which has no restraints, 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>432+<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>433 434 <h3 id="Passing-Route-Control">Passing Route Control</h3>435 436 <p>We may pass control to the next <em>matching</em> route, by calling the <em>third</em> argument,437 the <em>next()</em> function. When a match cannot be made, control is passed back to Connect,436-and middleware continue to be invoked. The same is true for several routes which have the same path defined, they will simply be executed in order until one does <em>not</em> call <em>next()</em>.</p>438+and middleware continue to be invoked in the order that they are added via <em>use()</em>. The same is true for several routes which have the same path defined, they will simply be executed in order until one does <em>not</em> call <em>next()</em> and decides to respond.</p>439 440 <pre><code>app.get('/users/:id?', function(req, res, next){441 var id = req.params.id;442 if (id) {7 unmodified lines450 // do something else451 });452 </code></pre>453 452-<p>Express 1.0 also introduces the <em>all()</em> method, which provides a route callback matching any HTTP method. This is useful in many ways, one example being the loading of resources before executing subsequent routes as shown below:</p>454+<p>The <em>app.all()</em> method is useful for applying the same logic for all HTTP verbs in a single call. Below we use this to load a user from our fake database, and assign it to <em>req.user</em>.</p>455 456 <pre><code>var express = require('express')457 , app = express.createServer();458 34 unmodified lines493 494 <pre><code> var express = require('express');495 496 var app = express.createServer(495- express.logger(),496- express.bodyDecoder()497+ express.logger()498+ , express.bodyParser()499 );500 </code></pre>501 500-<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks:</p>502+<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks, in a progressive manor.</p>503 504 <pre><code>app.use(express.logger({ format: ':method :uri' }));505 </code></pre>506 983 unmodified linesmodified:docs/guide.md+40 added-39 removed
1 2 ### Installation3 4-curl:5-6- $ curl -# http://expressjs.com/install.sh | sh7-8-npm:9-4 $ npm install express5 12-### Creating An Application6+### Creating A Server7 14-The _express.Server_ now inherits from _http.Server_, however15-follows the same idiom by providing _express.createServer()_ as shown below. This means16-that you can utilize Express server's transparently with other libraries.8+ To create an instance of the _express.HTTPServer_, simply invoke the _createServer()_ method. With our instance _app_ we can then define routes based on the HTTP verbs, in this example _app.get()_.9 10 var app = require('express').createServer();11 12 app.get('/', function(req, res){1 unmodified line14 });15 16 app.listen(3000);17 18+### Creating An HTTPS Server19+20+ To initialize a _express.HTTPSServer_ we do the same as above, however we21+ pass an options object, accepting _key_, _cert_ and the others mentioned in node's [https documentation](http://nodejs.org/docs/v0.3.7/api/https.html#https.createServer).22+ 23+ var app = require('express').createServer({ key: ... });24+25 ### Configuration26 27 Express supports arbitrary environments, such as _production_ and _development_. Developers28 can use the _configure()_ method to setup needs required by the current environment. When29 _configure()_ is called without an environment name it will be run in _every_ environment 30 prior to the environment specific callback.31 32 In the example below we only _dumpExceptions_, and respond with exception stack traces34-in _development_ mode, however for both environments we utilize _methodOverride_ and _bodyDecoder_.33+in _development_ mode, however for both environments we utilize _methodOverride_ and _bodyParser_.34 Note the use of _app.router_, which can (optionally) be used to mount the application routes,36-otherwise the first call to _app.{get,put,del,post}()_ will mount the routes.35+otherwise the first call to _app.get()_, _app.post()_, etc will mount the routes.36 37 app.configure(function(){38 app.use(express.methodOverride());40- app.use(express.bodyDecoder());39+ app.use(express.bodyParser());40 app.use(app.router);41 });42 43 app.configure('development', function(){45- app.use(express.staticProvider(__dirname + '/public'));44+ app.use(express.static(__dirname + '/public'));45 app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));46 });47 49- app.configure('production', function(){50- var oneYear = 31557600000;51- app.use(express.staticProvider({ root: __dirname + '/public', maxAge: oneYear }));52- app.use(express.errorHandler());53- });48+ app.configure('production', function(){49+ var oneYear = 31557600000;50+ app.use(express.static({ root: __dirname + '/public', maxAge: oneYear }));51+ app.use(express.errorHandler());52+ });53 54 For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:55 57- app.configure(function(){58- app.set('views', __dirname + '/views');59- app.set('views');60- // => "... views directory ..."61- 62- app.enable('some feature');63- // same as app.set('some feature', true);64- 65- app.disable('some feature');66- // same as app.set('some feature', false);67- });56+ app.configure(function(){57+ app.set('views', __dirname + '/views');58+ app.set('views');59+ // => "/absolute/path/to/views"60+ 61+ app.enable('some feature');62+ // same as app.set('some feature', true);63+ 64+ app.disable('some feature');65+ // same as app.set('some feature', false);66+ 67+ app.enabled('some feature')68+ // => false69+ });70 71 To alter the environment we can set the _NODE_ENV_ environment variable, for example:72 73 $ NODE_ENV=production node app.js3 unmodified lines77 ### Settings78 79 Express supports the following settings out of the box:80 79- * _env_ Application environment set internally, use _app.set('env')_ on _Server#listen()_81 * _home_ Application base path used for _res.redirect()_ and transparently handling mounted apps.82 * _views_ Root views directory defaulting to **CWD/views**83 * _view engine_ Default view engine name for views rendered without extensions84 * _view options_ An object specifying global view options66 unmodified lines151 "/user/:id.:format?"152 /user/12153 /user/12.json154 154-For example we can __POST__ some json, and echo the json back using the _bodyDecoder_ middleware which will parse json request bodies (as well as others), and place the result in _req.body_:155+For example we can __POST__ some json, and echo the json back using the _bodyParser_ middleware which will parse json request bodies (as well as others), and place the result in _req.body_:156 157 var express = require('express')158 , app = express.createServer();159 159- app.use(express.bodyDecoder());160+ app.use(express.bodyParser());161 162 app.post('/', function(req, res){163 res.send(req.body);164 });165 166 app.listen(3000);167 167-Express 2.0.0-pre also supports named capture groups. Typically we may use a "dump" placeholder such as "/user/:id" which has no restraints, however say for example we are limiting a user id to digits, we may use _'/user/:id(\\d+)'_ which will _not_ match unless the placeholder value contains only digits.168+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 _'/user/:id(\\d+)'_ which will _not_ match unless the placeholder value contains only digits.169 170 ### Passing Route Control171 172 We may pass control to the next _matching_ route, by calling the _third_ argument,173 the _next()_ function. When a match cannot be made, control is passed back to Connect,173-and middleware continue to be invoked. The same is true for several routes which have the same path defined, they will simply be executed in order until one does _not_ call _next()_.174+and middleware continue to be invoked in the order that they are added via _use()_. The same is true for several routes which have the same path defined, they will simply be executed in order until one does _not_ call _next()_ and decides to respond.175 176 app.get('/users/:id?', function(req, res, next){177 var id = req.params.id;178 if (id) {6 unmodified lines185 app.get('/users', function(req, res){186 // do something else187 });188 188-Express 1.0 also introduces the _all()_ method, which provides a route callback matching any HTTP method. This is useful in many ways, one example being the loading of resources before executing subsequent routes as shown below:189+The _app.all()_ method is useful for applying the same logic for all HTTP verbs in a single call. Below we use this to load a user from our fake database, and assign it to _req.user_.190 191 var express = require('express')192 , app = express.createServer();193 33 unmodified lines227 228 var express = require('express');229 230 var app = express.createServer(230- express.logger(),231- express.bodyDecoder()231+ express.logger()232+ , express.bodyParser()233 );234 234-Alternatively we can _use()_ them which is useful when adding middleware within _configure()_ blocks:235+Alternatively we can _use()_ them which is useful when adding middleware within _configure()_ blocks, in a progressive manor.236 237 app.use(express.logger({ format: ':method :uri' }));238 239 Typically with connect middleware you would _require('connect')_ like so:870 unmodified lines