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 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

3 files+104 added−106 removed

modified:docs/guide.1+32 added-37 removed
5 unmodified lines
6.SH "NAME"7\fBguide\fR8.9.SS "Installation"10curl:10.12.IP "" 413.11.nf1216$ curl \-# http://expressjs\.com/install\.sh | sh13$ npm install express14.15.fi16.20.IP "" 017.SS "Creating A Server"18To 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.P23npm:24.20.IP "" 421.22.nf2329$ npm install express24var app = require(\'express\')\.createServer();2526app\.get(\'/\', function(req, res){27 res\.send(\'hello world\');28});2930app\.listen(3000);31.32.fi33.34.IP "" 035.35.SS "Creating An Application"36The \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"37To 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.nf4242var app = require(\'express\')\.createServer();4344app\.get(\'/\', function(req, res){45 res\.send(\'hello world\');46});4748app\.listen(3000);43 var app = require(\'express\')\.createServer({ key: \.\.\. });44.45.fi46.47.IP "" 0
1 unmodified line
49.SS "Configuration"50Express 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.P58In 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\.53In 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.nf5859app\.configure(function(){60 app\.use(express\.methodOverride());66 app\.use(express\.bodyDecoder());61 app\.use(express\.bodyParser());62 app\.use(app\.router);63});6465app\.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});6970app\.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 lines
83.IP "" 484.85.nf8692app\.configure(function(){87 app\.configure(function(){88 app\.set(\'views\', __dirname + \'/views\');89 app\.set(\'views\');95 // => "\.\.\. views directory \.\.\."90 // => "/absolute/path/to/views"9192 app\.enable(\'some feature\');93 // same as app\.set(\'some feature\', true);9495 app\.disable(\'some feature\');96 // same as app\.set(\'some feature\', false);9798 app\.enabled(\'some feature\')99 // => false102});100 });101.102.fi103.104.IP "" 0
17 unmodified lines
122.SS "Settings"123Express 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\fR
112 unmodified lines
242.243.IP "" 0244.245.P251For 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:246For 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.nf251252var express = require(\'express\')253 , app = express\.createServer();254260app\.use(express\.bodyDecoder());255app\.use(express\.bodyParser());256257app\.post(\'/\', function(req, res){258 res\.send(req\.body);259});
4 unmodified lines
264.265.IP "" 0266.267.P273Express 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\.268Typically 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"276We 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\.271We 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.nf
15 unmodified lines
291.292.IP "" 0293.294.P300Express 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:295The \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.nf
43 unmodified lines
343344 var express = require(\'express\');345346var app = express\.createServer(352 express\.logger(),353 express\.bodyDecoder()347 express\.logger()348 , express\.bodyParser()349 );350.351.fi352.353.IP "" 0354.355.P361Alternatively we can \fIuse()\fR them which is useful when adding middleware within \fIconfigure()\fR blocks:356Alternatively we can \fIuse()\fR them which is useful when adding middleware within \fIconfigure()\fR blocks, in a progressive manor\.357.358.IP "" 4359.360.nf
1606 unmodified lines
modified:docs/guide.html+32 added-30 removed
178 unmodified lines
179 </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 lines
249 <code>guide</code>250</p>251<h3 id="Installation">Installation</h3>252252<p>curl:</p>253254<pre><code>$ curl -# http://expressjs.com/install.sh | sh255</code></pre>256257<p>npm:</p>258253<pre><code>$ npm install express254</code></pre>255262<h3 id="Creating-An-Application">Creating An Application</h3>256<h3 id="Creating-A-Server">Creating A Server</h3>257264<p>The <em>express.Server</em> now inherits from <em>http.Server</em>, however265follows the same idiom by providing <em>express.createServer()</em> as shown below. This means266that 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>259260<pre><code>var app = require('express').createServer();261262app.get('/', function(req, res){
2 unmodified lines
265266app.listen(3000);267</code></pre>268269<h3 id="Creating-An-HTTPS-Server">Creating An HTTPS Server</h3>270271<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>273274<pre><code> var app = require('express').createServer({ key: ... });275</code></pre>276277<h3 id="Configuration">Configuration</h3>278279<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers280can 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> environment282prior to the environment specific callback.</p>283284<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces285in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyDecoder</em>.285in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyParser</em>.286Note the use of <em>app.router</em>, which can (optionally) be used to mount the application routes,287otherwise the first call to <em>app.{get,put,del,post}()</em> will mount the routes.</p>287otherwise the first call to <em>app.get()</em>, <em>app.post()</em>, etc will mount the routes.</p>288289<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});294295app.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});299300app.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>306307<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>308309<pre><code>app.configure(function(){309<pre><code> app.configure(function(){310 app.set('views', __dirname + '/views');311 app.set('views');312 // =&gt; "... views directory ..."312 // =&gt; "/absolute/path/to/views"313314 app.enable('some feature');315 // same as app.set('some feature', true);316317 app.disable('some feature');318 // same as app.set('some feature', false);319320 app.enabled('some feature')321 // =&gt; false319});322 });323</code></pre>324325<p>To alter the environment we can set the <em>NODE_ENV</em> environment variable, for example:</p>326
6 unmodified lines
333334<p>Express supports the following settings out of the box:</p>335336<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 lines
414 /user/12415 /user/12.json416</code></pre>417416<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>419420<pre><code>var express = require('express')421 , app = express.createServer();422421app.use(express.bodyDecoder());423app.use(express.bodyParser());424425app.post('/', function(req, res){426 res.send(req.body);427});428429app.listen(3000);430</code></pre>431430<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>433434<h3 id="Passing-Route-Control">Passing Route Control</h3>435436<p>We may pass control to the next <em>matching</em> route, by calling the <em>third</em> argument,437the <em>next()</em> function. When a match cannot be made, control is passed back to Connect,436and 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>438and 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>439440<pre><code>app.get('/users/:id?', function(req, res, next){441 var id = req.params.id;442 if (id) {
7 unmodified lines
450 // do something else451});452</code></pre>453452<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>455456<pre><code>var express = require('express')457 , app = express.createServer();458
34 unmodified lines
493494<pre><code> var express = require('express');495496var app = express.createServer(495 express.logger(),496 express.bodyDecoder()497 express.logger()498 , express.bodyParser()499 );500</code></pre>501500<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>503504<pre><code>app.use(express.logger({ format: ':method :uri' }));505</code></pre>506
983 unmodified lines
modified:docs/guide.md+40 added-39 removed
12### Installation34curl:56 $ curl -# http://expressjs.com/install.sh | sh78npm:94 $ npm install express512### Creating An Application6### Creating A Server714The _express.Server_ now inherits from _http.Server_, however15follows the same idiom by providing _express.createServer()_ as shown below. This means16that 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()_.910 var app = require('express').createServer();11 12 app.get('/', function(req, res){
1 unmodified line
14 });15 16 app.listen(3000);1718### Creating An HTTPS Server1920 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: ... });2425### Configuration2627Express supports arbitrary environments, such as _production_ and _development_. Developers28can 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 30prior to the environment specific callback.3132In the example below we only _dumpExceptions_, and respond with exception stack traces34in _development_ mode, however for both environments we utilize _methodOverride_ and _bodyDecoder_.33in _development_ mode, however for both environments we utilize _methodOverride_ and _bodyParser_.34Note the use of _app.router_, which can (optionally) be used to mount the application routes,36otherwise the first call to _app.{get,put,del,post}()_ will mount the routes.35otherwise the first call to _app.get()_, _app.post()_, etc will mount the routes.3637 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 });5354For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:5557 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 });7071To alter the environment we can set the _NODE_ENV_ environment variable, for example:7273 $ NODE_ENV=production node app.js
3 unmodified lines
77### Settings7879Express supports the following settings out of the box:8079 * _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 options
66 unmodified lines
151 "/user/:id.:format?"152 /user/12153 /user/12.json154154For 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_:155For 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_:156157 var express = require('express')158 , app = express.createServer();159159 app.use(express.bodyDecoder());160 app.use(express.bodyParser());161162 app.post('/', function(req, res){163 res.send(req.body);164 });165166 app.listen(3000);167167Express 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.168Typically 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.169170### Passing Route Control171172We may pass control to the next _matching_ route, by calling the _third_ argument,173the _next()_ function. When a match cannot be made, control is passed back to Connect,173and 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()_.174and 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.175176 app.get('/users/:id?', function(req, res, next){177 var id = req.params.id;178 if (id) {
6 unmodified lines
185 app.get('/users', function(req, res){186 // do something else187 });188188Express 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:189The _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_.190191 var express = require('express')192 , app = express.createServer();193
33 unmodified lines
227228 var express = require('express');229230 var app = express.createServer(230 express.logger(),231 express.bodyDecoder()231 express.logger()232 , express.bodyParser()233 );234234Alternatively we can _use()_ them which is useful when adding middleware within _configure()_ blocks:235Alternatively we can _use()_ them which is useful when adding middleware within _configure()_ blocks, in a progressive manor.236237 app.use(express.logger({ format: ':method :uri' }));238239Typically with connect middleware you would _require('connect')_ like so:
870 unmodified lines