guide.html
This file
Path
docs/guide.html
321 lines · 8.9 KiB · HTML
Last changed by
Added contrib docsTj Holowaychukbefore adoptionf0ba57b
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322<html> <head> <title>Express - node web framework</title> <style> #header { position: absolute; top: 10px; left: 0; padding: 12px 0; text-indent: 40px; width: 100%; border-top: 1px solid rgba(0,0,0,0.7); border-bottom: 1px solid rgba(0,0,0,0.7); background: rgba(255,255,255,0.1) url(http://www.sencha.com/favicon.ico) no-repeat 15px 50%; text-align: left; color: #fff; } #tagline { margin-left: 75px; margin-bottom: 30px; color: rgba(255,255,255,0.7); } html { background: #1c1c1c url(images/bg.tile.jpg); }
body { margin: 0; padding-bottom: 30px; font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial"; font-size: 14px; line-height: 1.5; -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1); -moz-text-stroke: 1px rgba(0, 0, 0, 0.1); background: url(images/bg.jpg) 50% 0 no-repeat; color: #8b8b8b; }
* { outline: none; }
em { color: white; }
a img { border: none !important; }
a { font-weight: bold; color: white; -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow; -webkit-transition-duration: 0.15s; -webkit-transition-timing-function: ease-out; } a:hover { opacity: 0.8; }
h1, h2, h3 { margin: 45px 0 0 0; color: white; text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }
h3 { font-size: 18px; }
pre { margin: 20px 10px; padding: 25px 20px; background: rgba(0,0,0,0.5); border: 1px solid #323232; -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -webkit-border-radius: 5px; -moz-border-radius: 5px; }
code { font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }
ul { margin: 15px 0; padding: 0 0 0 35px; } ul li { margin: 0; padding: 2px 0; list-style: square; }
.sect { margin-left: 40px; }
#logo { display: block; margin-left: 30%; margin-bottom: 30px; width: 194px; height: 51px; background: url(images/logo.png) 0 0 no-repeat; text-indent: -99999px; } #logo:hover { opacity: 0.7; } #logo:active { opacity: 0.3; }
#ribbon { position: fixed; top: 0; right: 0; z-index: 2; }
#wrapper { width: 100%; min-height: 800px; background: url(images/top.png) 0 0 repeat-x; }
#container { margin: 0 auto; padding-top: 110px; width: 550px; }
#menu { margin-left: 65px; padding: 0; padding-bottom: 30px; } #menu li { display: inline; list-style: none; } #menu li a { display: block; float: left; margin: 0 2px; padding: 3px 15px; background: rgba(0,0,0,0.2); -webkit-border-radius: 8px; -moz-border-radius: 8px; -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow; -webkit-transition-duration: 0.15s; -webkit-transition-timing-function: ease-out; } #menu li a:hover, #menu li a.active { background: rgba(0,0,0,0.5); } #menu li a:active { background: rgba(0,0,0,0.1); -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); } </style> </head> <body> <a href='http://github.com/visionmedia/express'> <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> </a> <div id="header"><strong>Sencha</strong> labs</div> <div id="wrapper"> <div id="container"> <a href='http://github.com/visionmedia/express' id='logo'>Express</a> <p id="tagline"> High performance, high class web development for <a href="http://nodejs.org">Node.js</a> </p> <ul id="menu"> <li><a href="index.html">Home</a></li> <li><a href="guide.html">Guide</a></li> <li><a href="contrib.html">Contributing</a></li> <li><a href="migrate.html">1.x Migration</a></li> </ul><div class='mp'><h3 id="Creating-An-Application">Creating An Application</h3>
<p>The <em>express.Server</em> now inherits from <em>http.Server</em>, howeverfollows the same idiom by providing <em>express.createServer()</em> as shown below. This meansthat you can utilize Express server's transparently with other libraries.</p>
<pre><code>var app = require('express').createServer();
app.get('/', function(req, res){ res.send('hello world');});
app.listen(3000);</code></pre>
<h3 id="Configuration">Configuration</h3>
<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developerscan use the <em>configure()</em> method to setup needs required by the current environment. When<em>configure()</em> is called without an environment name it will be run in <em>every</em> environmentprior to the environment specific callback.</p>
<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack tracesin <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyDecoder</em>.</p>
<pre><code>app.configure(function(){ app.use('/', connect.methodOverride()); app.use('/', connect.bodyDecoder());});
app.configure('development', function(){ app.use('/', connect.errorHandler({ dumpExceptions: true, showStack: true }));});
app.configure('production', function(){ app.use('/', connect.errorHandler());});</code></pre>
<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>
<pre><code>app.configure(function(){ app.set('views', __dirname + '/views'); app.set('views'); // => "... views directory ..."
app.enable('some feature'); // same as app.set('some feature', true);
app.disable('some feature'); // same as app.set('some feature', false);});</code></pre>
<h3 id="Settings">Settings</h3>
<p>Express supports the following settings out of the box:</p>
<ul><li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li><li><em>view engine</em> Default view engine name for views rendered without extensions</li><li><em>reload views</em> Reloads altered views, by default watches for <em>mtime</em> changes with with a 5 minute interval. Example: <code>app.set('reload views', { interval: 60000 });</code></li></ul>
<h3 id="Routing">Routing</h3>
<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.For example we may want to render a user's account for the path <em>/user/12</em>, thiscan be done by defining the route below. The values associated to the named placeholders,are passed as the <em>third</em> argument, which here we name <em>params</em>.</p>
<pre><code>app.get('/user/:id', function(req, res, params){ res.send('user ' + params.id);});</code></pre>
<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For examplewhen <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>
<pre><code>\/user\/([^\/]+)\/?</code></pre>
<p>Literal regular expressions may also be passed for complex uses:</p>
<pre><code>app.get(/^\/foo(bar)?$/, function(){});</code></pre>
<p>Below are some route examples, and the associated paths that theymay consume:</p>
<pre><code> "/user/:id" /user/12
"/users/:id?" /users/5 /users
"/files/*" /files/jquery.js /files/javascripts/jquery.js
"/file/*.*" /files/jquery.js /files/javascripts/jquery.js
"/user/:id/:operation?" /user/1 /user/1/edit
"/products.:format" /products.json /products.xml
"/products.:format?" /products.json /products.xml /products</code></pre>
<h3 id="Passing-Route-Control">Passing Route Control</h3>
<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>
<pre><code>app.get('/users/:id?', function(req, res, params){ if (params.id) { // do something } else { next(); }});
app.get('/users', function(req, res, params){ // do something else});</code></pre>
<h3 id="Middleware">Middleware</h3>
<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/extjs/Connect">Connect</a> can bepassed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>
<pre><code>var connect = require('connect'), express = require('express');
var app = express.createServer( connect.logger(), connect.redirect(), connect.bodyDecoder());</code></pre>
</div> </div> </div> </body></html>