guide.html
This file
Path
docs/guide.html
320 lines · 8.8 KiB · HTML
Last changed by
Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
History of this fileTake it
Started guideTj Holowaychukbefore adoption9e3a0c6
lines 1–131why, one step back1<html>2 <head>3 <title>Express - node web framework</title>4 <style>5 #header {6 position: absolute;7 top: 10px;8 left: 0;9 padding: 12px 0;10 text-indent: 40px;11 width: 100%;12 border-top: 1px solid rgba(0,0,0,0.7);13 border-bottom: 1px solid rgba(0,0,0,0.7);14 background: rgba(255,255,255,0.1) url(http://www.sencha.com/favicon.ico) no-repeat 15px 50%;15 text-align: left;16 color: #fff;17 }18 #tagline {19 margin-left: 75px;20 margin-bottom: 30px;21 color: rgba(255,255,255,0.7); }22 html {23 background: #1c1c1c url(images/bg.tile.jpg); }2425 body {26 margin: 0;27 padding-bottom: 30px;28 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";29 font-size: 14px;30 line-height: 1.5;31 -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1);32 -moz-text-stroke: 1px rgba(0, 0, 0, 0.1);33 background: url(images/bg.jpg) 50% 0 no-repeat;34 color: #8b8b8b; }3536 * {37 outline: none; }3839 em {40 color: white; }4142 a img {43 border: none !important; }4445 a {46 font-weight: bold;47 color: white;48 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;49 -webkit-transition-duration: 0.15s;50 -webkit-transition-timing-function: ease-out; }51 a:hover {52 opacity: 0.8; }5354 h1, h2, h3 {55 margin: 45px 0 0 0;56 color: white;57 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }5859 h3 {60 font-size: 18px; }6162 pre {63 margin: 20px 10px;64 padding: 25px 20px;65 background: rgba(0,0,0,0.5);66 border: 1px solid #323232;67 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);68 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);69 -webkit-border-radius: 5px;70 -moz-border-radius: 5px; }7172 code {73 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }7475 ul {76 margin: 15px 0;77 padding: 0 0 0 35px; }78 ul li {79 margin: 0;80 padding: 2px 0;81 list-style: square; }8283 .sect {84 margin-left: 40px; }8586 #logo {87 display: block;88 margin-left: 30%;89 margin-bottom: 30px;90 width: 194px;91 height: 51px;92 background: url(images/logo.png) 0 0 no-repeat;93 text-indent: -99999px; }94 #logo:hover {95 opacity: 0.7; }96 #logo:active {97 opacity: 0.3; }9899 #ribbon {100 position: fixed;101 top: 0;102 right: 0;103 z-index: 2; }104105 #wrapper {106 width: 100%;107 min-height: 800px;108 background: url(images/top.png) 0 0 repeat-x; }109110 #container {111 margin: 0 auto;112 padding-top: 110px;113 width: 550px; }114115 #menu {116 margin-left: 130px;117 padding: 0;118 padding-bottom: 30px; }119 #menu li {120 display: inline;121 list-style: none; }122 #menu li a {123 display: block;124 float: left;125 margin: 0 2px;126 padding: 3px 15px;127 background: rgba(0,0,0,0.2);128 -webkit-border-radius: 8px;129 -moz-border-radius: 8px;130 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);131 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
line 132why, one step back132 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;Started guideTj Holowaychukbefore adoption9e3a0c6
lines 133–161why, one step back133 -webkit-transition-duration: 0.15s;134 -webkit-transition-timing-function: ease-out; }135 #menu li a:hover,136 #menu li a.active {137 background: rgba(0,0,0,0.5); }138 #menu li a:active {139 background: rgba(0,0,0,0.1);140 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);141 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }142 </style>143 </head>144 <body>145 <a href='http://github.com/visionmedia/express'> 146 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 147 </a>148 <div id="header"><strong>Sencha</strong> labs</div>149 <div id="wrapper">150 <div id="container">151 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 152 <p id="tagline">153 High performance, high class web development for154 <a href="http://nodejs.org">Node.js</a>155 </p>156 <ul id="menu">157 <li><a href="index.html">Home</a></li>158 <li><a href="guide.html">Guide</a></li>159 <li><a href="migrate.html">1.x Migration</a></li>160 </ul>161<div class='mp'>Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
line 162why, one step back162<h3 id="Creating-An-Application">Creating An Application</h3>Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 163–164why, one step back163164<p>The <em>express.Server</em> now inherits from <em>http.Server</em>, howeverMoved config docs upTj Holowaychukbefore adoption6e8413f
lines 165–166why, one step back165follows the same idiom by providing <em>express.createServer()</em> as shown below. This means166that you can utilize Express server's transparently with other libraries.</p>Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 167–176why, one step back167168<pre><code>var app = require('express').createServer();169170app.get('/', function(req, res){171 res.send('hello world');172});173174app.listen(3000);175</code></pre>176Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 177–222why, one step back177<h3 id="Configuration">Configuration</h3>178179<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers180can use the <em>configure()</em> method to setup needs required by the current environment. When181<em>configure()</em> is called without an environment name it will be run in <em>every</em> environment182prior to the environment specific callback.</p>183184<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces185in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyDecoder</em>.</p>186187<pre><code>app.configure(function(){188 app.use('/', connect.methodOverride());189 app.use('/', connect.bodyDecoder());190});191192app.configure('development', function(){193 app.use('/', connect.errorHandler({ dumpExceptions: true, showStack: true }));194});195196app.configure('production', function(){197 app.use('/', connect.errorHandler());198});199</code></pre>200201<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>202203<pre><code>app.configure(function(){204 app.set('views', __dirname + '/views');205 app.set('views');206 // => "... views directory ..."207208 app.enable('some feature');209 // same as app.set('some feature', true);210211 app.disable('some feature');212 // same as app.set('some feature', false);213});214</code></pre>215216<h3 id="Settings">Settings</h3>217218<p>Express supports the following settings out of the box:</p>219220<ul>221<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>222<li><em>view engine</em> Default view engine name for views rendered without extensions</li>Added more setting docsTj Holowaychukbefore adoption38281e6
lines 223–224why, one step back223<li><em>reload views</em> Reloads altered views, by default watches for <em>mtime</em> changes with224 with a 5 minute interval. Example: <code>app.set('reload views', { interval: 60000 });</code></li>Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 225–227why, one step back225</ul>226227Started guideTj Holowaychukbefore adoption9e3a0c6
lines 228–231why, one step back228<h3 id="Routing">Routing</h3>229230<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.231For example we may want to render a user's account for the path <em>/user/12</em>, thisRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 232–233why, one step back232can be done by defining the route below. The values associated to the named placeholders,233are passed as the <em>third</em> argument, which here we name <em>params</em>.</p>Started guideTj Holowaychukbefore adoption9e3a0c6
lines 234–280why, one step back234235<pre><code>app.get('/user/:id', function(req, res, params){236 res.send('user ' + params.id);237});238</code></pre>239240<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example241when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>242243<pre><code>\/user\/([^\/]+)\/?244</code></pre>245246<p>Literal regular expressions may also be passed for complex uses:</p>247248<pre><code>app.get(/^\/foo(bar)?$/, function(){});249</code></pre>250251<p>Below are some route examples, and the associated paths that they252may consume:</p>253254<pre><code> "/user/:id"255 /user/12256257 "/users/:id?"258 /users/5259 /users260261 "/files/*"262 /files/jquery.js263 /files/javascripts/jquery.js264265 "/file/*.*"266 /files/jquery.js267 /files/javascripts/jquery.js268269 "/user/:id/:operation?"270 /user/1271 /user/1/edit272273 "/products.:format"274 /products.json275 /products.xml276277 "/products.:format?"278 /products.json279 /products.xml280 /productsRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 281–297why, one step back281</code></pre>282283<h3 id="Passing-Route-Control">Passing Route Control</h3>284285<p>We may pass control to the next <em>matching</em> route, by calling the <em>fourth</em> parameter,286the <em>next()</em> function. When a match cannot be made, control is passed back to Connect.</p>287288<pre><code>app.get('/users/:id?', function(req, res, params){289 if (params.id) {290 // do something291 } else {292 next();293 }294});295296app.get('/users', function(req, res, params){297 // do something elseAdded configuration sectionTj Holowaychukbefore adoption83d2555
lines 298–314why, one step back298});299</code></pre>300301<h3 id="Middleware">Middleware</h3>302303<p>The Express <em>Plugin</em> is no more! middleware via <a href="http://github.com/extjs/Connect">Connect</a> can be304passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>305306<pre><code>var connect = require('connect'),307 express = require('express');308309var app = express.createServer(310 connect.logger(),311 connect.redirect(),312 connect.bodyDecoder()313);314</code></pre>Configuration docsTj Holowaychukbefore adoptioncb04364
line 315why, one step back315Started guideTj Holowaychukbefore adoption9e3a0c6
lines 316–320why, one step back316</div>317 </div>318 </div>319 </body>320</html>