removed old 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 785ac87 · Browse files · 31 files·+0 added−5316 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
31 files+0 added−5316 removed
- other:+0 added−5316 removed
Provenance
- Commit
856f3b3- Tree
93f6489- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
785ac87
31 files changed+0 added−5316 removed
deleted:docs/images/apps/clickdummy.pngbinary
A binary file, deleted.
deleted:docs/images/apps/developmentseed.pngbinary
A binary file, deleted.
deleted:docs/images/apps/e-resistable.pngbinary
A binary file, deleted.
deleted:docs/images/apps/learnboost.pngbinary
A binary file, deleted.
deleted:docs/images/apps/markupio.pngbinary
A binary file, deleted.
deleted:docs/images/apps/nodeko.pngbinary
A binary file, deleted.
deleted:docs/images/apps/opowerjobs.pngbinary
A binary file, deleted.
deleted:docs/images/apps/scrabbly.pngbinary
A binary file, deleted.
deleted:docs/images/apps/storify.pngbinary
A binary file, deleted.
deleted:docs/images/apps/toptwittertrends.pngbinary
A binary file, deleted.
deleted:docs/images/apps/widescript.pngbinary
A binary file, deleted.
deleted:docs/images/bg.jpgbinary
A binary file, deleted.
deleted:docs/images/bg.tile.jpgbinary
A binary file, deleted.
deleted:docs/images/logo.pngbinary
A binary file, deleted.
deleted:docs/images/top.pngbinary
A binary file, deleted.
deleted:docs/layout/foot.html-4 removed
1234 </div> </div> </body></html>No newline at end of filedeleted:docs/layout/head.html-193 removed
1-<html>2- <head>3- <title>Express - node web framework</title>4- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">5- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>6- <style>7- #tagline {8- margin-left: 75px;9- margin-bottom: 30px;10- color: rgba(255,255,255,0.7); }11- html {12- background: #1c1c1c url(images/bg.tile.jpg); }13-14- body {15- margin: 0;16- padding-bottom: 30px;17- font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";18- font-size: 14px;19- line-height: 1.5;20- -webkit-font-smoothing: antialiased;21- background: url(images/bg.jpg) 50% 0 no-repeat;22- color: #8b8b8b; }23-24- * {25- outline: none; }26-27- em {28- color: white; }29-30- a img {31- border: none !important; }32-33- a {34- font-weight: bold;35- text-decoration: none;36- color: white;37- -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;38- -webkit-transition-duration: 0.15s;39- -webkit-transition-timing-function: ease-out; }40- a:hover {41- opacity: 0.8; }42-43- h1, h2, h3, h4 {44- margin: 45px 0 0 0;45- color: white;46- text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }47-48- h3 {49- font-size: 18px; }50- h4 {51- margin-left: 10px;52- font-size: 14px;53- }54-55- pre {56- margin: 20px 10px;57- padding: 25px 20px;58- background: rgba(0,0,0,0.5);59- border: 1px solid #323232;60- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);62- -webkit-border-radius: 5px;63- -moz-border-radius: 5px; }64-65- code {66- font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }67-68- ul {69- margin: 15px 0;70- padding: 0 0 0 35px; }71- ul li {72- margin: 0;73- padding: 2px 0;74- list-style: square; }75- ul li ul {76- margin: 0;77- padding-left: 12px;78- }79-80- .man-name, #Express { display:none; }81-82- .sect {83- margin-left: 40px; }84- img {85- margin-left: 20px;86- margin-bottom: 15px;87- }88-89- #logo {90- display: block;91- margin-left: 30%;92- margin-bottom: 30px;93- width: 194px;94- height: 51px;95- background: url(images/logo.png) 0 0 no-repeat;96- text-indent: -99999px; }97- #logo:hover {98- opacity: 0.7; }99- #logo:active {100- opacity: 0.3; }101-102- #ribbon {103- position: fixed;104- top: 0;105- right: 0;106- z-index: 2; }107-108- #wrapper {109- width: 100%;110- min-height: 800px;111- background: url(images/top.png) 0 0 repeat-x; }112-113- #container {114- margin: 0 auto;115- padding-top: 80px;116- width: 550px; }117-118- #toc {119- position: fixed;120- top: 0;121- left: 0;122- margin: 0 0 0 15px;123- padding: 15px;124- height: 100%;125- background: rgba(0,0,0,0.2);126- overflow: auto;127- border-right: 1px solid rgba(255,255,255,0.05);128- }129- #toc li {130- padding: 0;131- list-style: none;132- }133- #toc li a {134- font-size: 11px;135- }136- #menu {137- margin-left: 75px;138- padding: 0;139- padding-bottom: 30px; }140- #menu li {141- display: inline;142- list-style: none; }143- #menu li a {144- display: block;145- float: left;146- margin: 0 2px;147- padding: 3px 15px;148- background: rgba(0,0,0,0.2);149- -webkit-border-radius: 8px;150- -moz-border-radius: 8px;151- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);153- -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;154- -webkit-transition-duration: 0.15s;155- -webkit-transition-timing-function: ease-out; }156- #menu li a:hover,157- #menu li a.active {158- background: rgba(0,0,0,0.5); }159- #menu li a:active {160- background: rgba(0,0,0,0.1);161- -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);162- -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }163- </style>164- <script>165- $(function(){166- $('.section').hide();167- $('.toggle, a.section-title').toggle(function(){168- $(this).siblings('ul').fadeIn(300);169- return false;170- }, function(){171- $(this).siblings('ul').fadeOut(300);172- return false;173- });174- });175- </script>176- </head>177- <body>178- <a href='http://github.com/visionmedia/express'> 179- <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 180- </a>181- <div id="wrapper">182- <div id="container">183- <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 184- <p id="tagline">185- High performance, high class web development for186- <a href="http://nodejs.org">Node.js</a>187- </p>188- <ul id="menu">189- <li><a href="index.html">Home</a></li>190- <li><a href="guide.html">Guide</a></li>191- <li><a href="screencasts.html">Screencasts</a></li>192- <li><a href="applications.html">Applications</a></li>193- </ul>deleted:docs/applications.html-246 removed
1-<html>2- <head>3- <title>Express - node web framework</title>4- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">5- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>6- <style>7- #tagline {8- margin-left: 75px;9- margin-bottom: 30px;10- color: rgba(255,255,255,0.7); }11- html {12- background: #1c1c1c url(images/bg.tile.jpg); }13-14- body {15- margin: 0;16- padding-bottom: 30px;17- font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";18- font-size: 14px;19- line-height: 1.5;20- -webkit-font-smoothing: antialiased;21- background: url(images/bg.jpg) 50% 0 no-repeat;22- color: #8b8b8b; }23-24- * {25- outline: none; }26-27- em {28- color: white; }29-30- a img {31- border: none !important; }32-33- a {34- font-weight: bold;35- text-decoration: none;36- color: white;37- -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;38- -webkit-transition-duration: 0.15s;39- -webkit-transition-timing-function: ease-out; }40- a:hover {41- opacity: 0.8; }42-43- h1, h2, h3, h4 {44- margin: 45px 0 0 0;45- color: white;46- text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }47-48- h3 {49- font-size: 18px; }50- h4 {51- margin-left: 10px;52- font-size: 14px;53- }54-55- pre {56- margin: 20px 10px;57- padding: 25px 20px;58- background: rgba(0,0,0,0.5);59- border: 1px solid #323232;60- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);62- -webkit-border-radius: 5px;63- -moz-border-radius: 5px; }64-65- code {66- font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }67-68- ul {69- margin: 15px 0;70- padding: 0 0 0 35px; }71- ul li {72- margin: 0;73- padding: 2px 0;74- list-style: square; }75- ul li ul {76- margin: 0;77- padding-left: 12px;78- }79-80- .man-name, #Express { display:none; }81-82- .sect {83- margin-left: 40px; }84- img {85- margin-left: 20px;86- margin-bottom: 15px;87- }88-89- #logo {90- display: block;91- margin-left: 30%;92- margin-bottom: 30px;93- width: 194px;94- height: 51px;95- background: url(images/logo.png) 0 0 no-repeat;96- text-indent: -99999px; }97- #logo:hover {98- opacity: 0.7; }99- #logo:active {100- opacity: 0.3; }101-102- #ribbon {103- position: fixed;104- top: 0;105- right: 0;106- z-index: 2; }107-108- #wrapper {109- width: 100%;110- min-height: 800px;111- background: url(images/top.png) 0 0 repeat-x; }112-113- #container {114- margin: 0 auto;115- padding-top: 80px;116- width: 550px; }117-118- #toc {119- position: fixed;120- top: 0;121- left: 0;122- margin: 0 0 0 15px;123- padding: 15px;124- height: 100%;125- background: rgba(0,0,0,0.2);126- overflow: auto;127- border-right: 1px solid rgba(255,255,255,0.05);128- }129- #toc li {130- padding: 0;131- list-style: none;132- }133- #toc li a {134- font-size: 11px;135- }136- #menu {137- margin-left: 75px;138- padding: 0;139- padding-bottom: 30px; }140- #menu li {141- display: inline;142- list-style: none; }143- #menu li a {144- display: block;145- float: left;146- margin: 0 2px;147- padding: 3px 15px;148- background: rgba(0,0,0,0.2);149- -webkit-border-radius: 8px;150- -moz-border-radius: 8px;151- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);153- -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;154- -webkit-transition-duration: 0.15s;155- -webkit-transition-timing-function: ease-out; }156- #menu li a:hover,157- #menu li a.active {158- background: rgba(0,0,0,0.5); }159- #menu li a:active {160- background: rgba(0,0,0,0.1);161- -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);162- -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }163- </style>164- <script>165- $(function(){166- $('.section').hide();167- $('.toggle, a.section-title').toggle(function(){168- $(this).siblings('ul').fadeIn(300);169- return false;170- }, function(){171- $(this).siblings('ul').fadeOut(300);172- return false;173- });174- });175- </script>176- </head>177- <body>178- <a href='http://github.com/visionmedia/express'> 179- <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 180- </a>181- <div id="wrapper">182- <div id="container">183- <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 184- <p id="tagline">185- High performance, high class web development for186- <a href="http://nodejs.org">Node.js</a>187- </p>188- <ul id="menu">189- <li><a href="index.html">Home</a></li>190- <li><a href="guide.html">Guide</a></li>191- <li><a href="screencasts.html">Screencasts</a></li>192- <li><a href="applications.html">Applications</a></li>193- </ul>194-195-196-<br />197-198-199-<p><a href="http://learnboost.com">Learnboost</a> is a free online gradebook application, aimed to crush the competition with innovative, realtime, enjoyable features.</p>200-201-<p><a href="http://learnboost.com"><img src="images/apps/learnboost.png" alt="LearnBoost" /></a></p>202-203-<p><a href="http://storify.com">Storify</a> lets you turn what people post on social media websites into compelling stories.</p>204-205-<p><a href="http://storify.com"><img src="images/apps/storify.png" alt="Storify" /></a></p>206-207-<p><a href="http://pakistansurvey.org/">Pakistan Survey</a> by <a href="http://developmentseed.org">Development Seed</a>, provides in-depth agency-specific analysis from regional experts with data from 1,000 interviews across 120 villages in all seven tribal agencies and mapping of 142 reported drone strikes in FATA through July 2010.</p>208-209-<p><a href="http://pakistansurvey.org"><img src="images/apps/developmentseed.png" alt="Pakistan Survey" /></a></p>210-211-<p><a href="http://markup.io">Markup.IO</a> allows you to draw directly on <em>any</em> website, then share with others to share your thoughts.</p>212-213-<p><a href="http://markup.io"><img src="images/apps/markupio.png" alt="Markup.IO" /></a></p>214-215-<p><a href="http://scrabb.ly">Scrabb.ly</a> is a massively multiplayer scrabble game initially created for the <a href="http://nodeknockout.com/">Node Knockout</a> competition.</p>216-217-<p><a href="http://scrabb.ly"><img src="images/apps/scrabbly.png" alt="Online Realtime Scrabble" /></a></p>218-219-<p><a href="http://clickdummy.net/">ClickDummy</a> is a rapid mockup prototyping application for designers and dummies.</p>220-221-<p><a href="http://clickdummy.net"><img src="images/apps/clickdummy.png" alt="Mockup Prototying" /></a></p>222-223-<p><a href="http://nodeknockout.com">Node Knockout</a> organized the first ever node-specific competition with hundreds of contestants.</p>224-225-<p><a href="http://nodeknockout.com"><img src="images/apps/nodeko.png" alt="Node Knockout Competition Express" /></a></p>226-227-<p><a href="http://widescript.com">Widescript</a> is an innovative app that helps you focus and interact with your texts – on your desktop, your couch or on the go.</p>228-229-<p><a href="http://widescript.com"><img src="images/apps/widescript.png" alt="Widescript" /></a></p>230-231-<p><a href="http://www.e-resistible.co.uk/">e-resistable</a> is an online order takeaway system providing an intuitive way to fill your belly from your computer!</p>232-233-<p><a href="http://www.e-resistible.co.uk"><img src="images/apps/e-resistable.png" alt="Online Takeaway" /></a></p>234-235-<p><a href="http://toptwittertrends.com">Top Twitter Trends</a> utilizes MongoDB, Socket.IO, jQuery and many other exciting libraries to bring you trending tweets in realtime.</p>236-237-<p><a href="http://toptwittertrends.com"><img src="images/apps/toptwittertrends.png" alt="Twitter Trends" /></a></p>238-239-<br />240-241-242-<p>The applications shown above are not listed in any specific order. To have an application added or removed please contact <a href="http://github.com/visionmedia">TJ Holowaychuk</a>.</p>243- </div>244- </div>245- </body>246-</html>deleted:docs/applications.md-47 removed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
<br />
[Learnboost](http://learnboost.com) is a free online gradebook application, aimed to crush the competition with innovative, realtime, enjoyable features.
[](http://learnboost.com)
[Storify](http://storify.com) lets you turn what people post on social media websites into compelling stories.
[](http://storify.com)
[Pakistan Survey](http://pakistansurvey.org/) by [Development Seed](http://developmentseed.org), provides in-depth agency-specific analysis from regional experts with data from 1,000 interviews across 120 villages in all seven tribal agencies and mapping of 142 reported drone strikes in FATA through July 2010.
[](http://pakistansurvey.org)
[Markup.IO](http://markup.io) allows you to draw directly on _any_ website, then share with others to share your thoughts.
[](http://markup.io)
[Scrabb.ly](http://scrabb.ly) is a massively multiplayer scrabble game initially created for the [Node Knockout](http://nodeknockout.com/) competition.
[](http://scrabb.ly)
[ClickDummy](http://clickdummy.net/) is a rapid mockup prototyping application for designers and dummies.
[](http://clickdummy.net)
[Node Knockout](http://nodeknockout.com) organized the first ever node-specific competition with hundreds of contestants.
[](http://nodeknockout.com)
[Widescript](http://widescript.com) is an innovative app that helps you focus and interact with your texts - on your desktop, your couch or on the go.
[](http://widescript.com)
[e-resistable](http://www.e-resistible.co.uk/) is an online order takeaway system providing an intuitive way to fill your belly from your computer!
[](http://www.e-resistible.co.uk)
[Top Twitter Trends](http://toptwittertrends.com) utilizes MongoDB, Socket.IO, jQuery and many other exciting libraries to bring you trending tweets in realtime.
[](http://toptwittertrends.com)
<br />
The applications shown above are not listed in any specific order. To have an application added or removed please contact [TJ Holowaychuk](http://github.com/visionmedia).No newline at end of filedeleted:docs/contrib.html-240 removed
1-<html>2- <head>3- <title>Express - node web framework</title>4- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">5- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>6- <style>7- #tagline {8- margin-left: 75px;9- margin-bottom: 30px;10- color: rgba(255,255,255,0.7); }11- html {12- background: #1c1c1c url(images/bg.tile.jpg); }13-14- body {15- margin: 0;16- padding-bottom: 30px;17- font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";18- font-size: 14px;19- line-height: 1.5;20- -webkit-font-smoothing: antialiased;21- background: url(images/bg.jpg) 50% 0 no-repeat;22- color: #8b8b8b; }23-24- * {25- outline: none; }26-27- em {28- color: white; }29-30- a img {31- border: none !important; }32-33- a {34- font-weight: bold;35- text-decoration: none;36- color: white;37- -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;38- -webkit-transition-duration: 0.15s;39- -webkit-transition-timing-function: ease-out; }40- a:hover {41- opacity: 0.8; }42-43- h1, h2, h3, h4 {44- margin: 45px 0 0 0;45- color: white;46- text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }47-48- h3 {49- font-size: 18px; }50- h4 {51- margin-left: 10px;52- font-size: 14px;53- }54-55- pre {56- margin: 20px 10px;57- padding: 25px 20px;58- background: rgba(0,0,0,0.5);59- border: 1px solid #323232;60- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);62- -webkit-border-radius: 5px;63- -moz-border-radius: 5px; }64-65- code {66- font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }67-68- ul {69- margin: 15px 0;70- padding: 0 0 0 35px; }71- ul li {72- margin: 0;73- padding: 2px 0;74- list-style: square; }75- ul li ul {76- margin: 0;77- padding-left: 12px;78- }79-80- .man-name, #Express { display:none; }81-82- .sect {83- margin-left: 40px; }84- img {85- margin-left: 20px;86- margin-bottom: 15px;87- }88-89- #logo {90- display: block;91- margin-left: 30%;92- margin-bottom: 30px;93- width: 194px;94- height: 51px;95- background: url(images/logo.png) 0 0 no-repeat;96- text-indent: -99999px; }97- #logo:hover {98- opacity: 0.7; }99- #logo:active {100- opacity: 0.3; }101-102- #ribbon {103- position: fixed;104- top: 0;105- right: 0;106- z-index: 2; }107-108- #wrapper {109- width: 100%;110- min-height: 800px;111- background: url(images/top.png) 0 0 repeat-x; }112-113- #container {114- margin: 0 auto;115- padding-top: 80px;116- width: 550px; }117-118- #toc {119- position: fixed;120- top: 0;121- left: 0;122- margin: 0 0 0 15px;123- padding: 15px;124- height: 100%;125- background: rgba(0,0,0,0.2);126- overflow: auto;127- border-right: 1px solid rgba(255,255,255,0.05);128- }129- #toc li {130- padding: 0;131- list-style: none;132- }133- #toc li a {134- font-size: 11px;135- }136- #menu {137- margin-left: 75px;138- padding: 0;139- padding-bottom: 30px; }140- #menu li {141- display: inline;142- list-style: none; }143- #menu li a {144- display: block;145- float: left;146- margin: 0 2px;147- padding: 3px 15px;148- background: rgba(0,0,0,0.2);149- -webkit-border-radius: 8px;150- -moz-border-radius: 8px;151- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);153- -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;154- -webkit-transition-duration: 0.15s;155- -webkit-transition-timing-function: ease-out; }156- #menu li a:hover,157- #menu li a.active {158- background: rgba(0,0,0,0.5); }159- #menu li a:active {160- background: rgba(0,0,0,0.1);161- -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);162- -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }163- </style>164- <script>165- $(function(){166- $('.section').hide();167- $('.toggle, a.section-title').toggle(function(){168- $(this).siblings('ul').fadeIn(300);169- return false;170- }, function(){171- $(this).siblings('ul').fadeOut(300);172- return false;173- });174- });175- </script>176- </head>177- <body>178- <a href='http://github.com/visionmedia/express'> 179- <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 180- </a>181- <div id="wrapper">182- <div id="container">183- <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 184- <p id="tagline">185- High performance, high class web development for186- <a href="http://nodejs.org">Node.js</a>187- </p>188- <ul id="menu">189- <li><a href="index.html">Home</a></li>190- <li><a href="guide.html">Guide</a></li>191- <li><a href="screencasts.html">Screencasts</a></li>192- <li><a href="applications.html">Applications</a></li>193- </ul>194-<h3>Development Dependencies</h3>195-196-<p>First install the dev dependencies by executing the following command in the repo’s directory:</p>197-198-<pre><code>$ npm install199-</code></pre>200-201-<h3>Running Tests</h3>202-203-<p>Express uses the <a href="http://github.com/visionmedia/expresso">Expresso</a> TDD204-framework to write and run elegant test suites extremely fast. To run all test suites205-simply execute:</p>206-207-<pre><code>$ make test208-</code></pre>209-210-<p>To target specific suites we may specify the files via:</p>211-212-<pre><code>$ make test TESTS=test/view.test.js213-</code></pre>214-215-<p>To check test coverage run:</p>216-217-<pre><code>$ make test-cov218-</code></pre>219-220-<h3>Contributions</h3>221-222-<p>To accept a contribution, you should follow these guidelines:</p>223-224-<ul>225-<li>All tests <em>must</em> pass</li>226-<li>Your alterations or additions <em>must</em> include tests</li>227-<li>Your commit(s) should be <em>focused</em>, do not commit once for several changes</li>228-<li>Do <em>not</em> alter release information such as the <em>version</em>, or <em>History.md</em></li>229-<li>Indents are <em>2</em> spaces.</li>230-</ul>231-232-233-<h3>Documentation</h3>234-235-<p>To contribute documentation edit the markdown files in <em>./docs</em>, however236-do <em>not</em> run <em>make docs</em>, as they will be re-built and published with each release.</p>237- </div>238- </div>239- </body>240-</html>deleted:docs/contrib.md-37 removed
12345678910111213141516171819202122232425262728293031323334353637
### Development Dependencies
First install the dev dependencies by executing the following command in the repo's directory:
$ npm install
### Running Tests
Express uses the [Expresso](http://github.com/visionmedia/expresso) TDDframework to write and run elegant test suites extremely fast. To run all test suitessimply execute:
$ make test
To target specific suites we may specify the files via:
$ make test TESTS=test/view.test.js
To check test coverage run:
$ make test-cov
### Contributions
To accept a contribution, you should follow these guidelines:
* All tests _must_ pass * Your alterations or additions _must_ include tests * Your commit(s) should be _focused_, do not commit once for several changes * Do _not_ alter release information such as the _version_, or _History.md_ * Indents are _2_ spaces.
### Documentation
To contribute documentation edit the markdown files in _./docs_, howeverdo _not_ run _make docs_, as they will be re-built and published with each release.No newline at end of filedeleted:docs/executable.html-216 removed
1-<html>2- <head>3- <title>Express - node web framework</title>4- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">5- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>6- <style>7- #tagline {8- margin-left: 75px;9- margin-bottom: 30px;10- color: rgba(255,255,255,0.7); }11- html {12- background: #1c1c1c url(images/bg.tile.jpg); }13-14- body {15- margin: 0;16- padding-bottom: 30px;17- font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";18- font-size: 14px;19- line-height: 1.5;20- -webkit-font-smoothing: antialiased;21- background: url(images/bg.jpg) 50% 0 no-repeat;22- color: #8b8b8b; }23-24- * {25- outline: none; }26-27- em {28- color: white; }29-30- a img {31- border: none !important; }32-33- a {34- font-weight: bold;35- text-decoration: none;36- color: white;37- -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;38- -webkit-transition-duration: 0.15s;39- -webkit-transition-timing-function: ease-out; }40- a:hover {41- opacity: 0.8; }42-43- h1, h2, h3, h4 {44- margin: 45px 0 0 0;45- color: white;46- text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }47-48- h3 {49- font-size: 18px; }50- h4 {51- margin-left: 10px;52- font-size: 14px;53- }54-55- pre {56- margin: 20px 10px;57- padding: 25px 20px;58- background: rgba(0,0,0,0.5);59- border: 1px solid #323232;60- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);62- -webkit-border-radius: 5px;63- -moz-border-radius: 5px; }64-65- code {66- font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }67-68- ul {69- margin: 15px 0;70- padding: 0 0 0 35px; }71- ul li {72- margin: 0;73- padding: 2px 0;74- list-style: square; }75- ul li ul {76- margin: 0;77- padding-left: 12px;78- }79-80- .man-name, #Express { display:none; }81-82- .sect {83- margin-left: 40px; }84- img {85- margin-left: 20px;86- margin-bottom: 15px;87- }88-89- #logo {90- display: block;91- margin-left: 30%;92- margin-bottom: 30px;93- width: 194px;94- height: 51px;95- background: url(images/logo.png) 0 0 no-repeat;96- text-indent: -99999px; }97- #logo:hover {98- opacity: 0.7; }99- #logo:active {100- opacity: 0.3; }101-102- #ribbon {103- position: fixed;104- top: 0;105- right: 0;106- z-index: 2; }107-108- #wrapper {109- width: 100%;110- min-height: 800px;111- background: url(images/top.png) 0 0 repeat-x; }112-113- #container {114- margin: 0 auto;115- padding-top: 80px;116- width: 550px; }117-118- #toc {119- position: fixed;120- top: 0;121- left: 0;122- margin: 0 0 0 15px;123- padding: 15px;124- height: 100%;125- background: rgba(0,0,0,0.2);126- overflow: auto;127- border-right: 1px solid rgba(255,255,255,0.05);128- }129- #toc li {130- padding: 0;131- list-style: none;132- }133- #toc li a {134- font-size: 11px;135- }136- #menu {137- margin-left: 75px;138- padding: 0;139- padding-bottom: 30px; }140- #menu li {141- display: inline;142- list-style: none; }143- #menu li a {144- display: block;145- float: left;146- margin: 0 2px;147- padding: 3px 15px;148- background: rgba(0,0,0,0.2);149- -webkit-border-radius: 8px;150- -moz-border-radius: 8px;151- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);153- -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;154- -webkit-transition-duration: 0.15s;155- -webkit-transition-timing-function: ease-out; }156- #menu li a:hover,157- #menu li a.active {158- background: rgba(0,0,0,0.5); }159- #menu li a:active {160- background: rgba(0,0,0,0.1);161- -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);162- -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }163- </style>164- <script>165- $(function(){166- $('.section').hide();167- $('.toggle, a.section-title').toggle(function(){168- $(this).siblings('ul').fadeIn(300);169- return false;170- }, function(){171- $(this).siblings('ul').fadeOut(300);172- return false;173- });174- });175- </script>176- </head>177- <body>178- <a href='http://github.com/visionmedia/express'> 179- <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 180- </a>181- <div id="wrapper">182- <div id="container">183- <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 184- <p id="tagline">185- High performance, high class web development for186- <a href="http://nodejs.org">Node.js</a>187- </p>188- <ul id="menu">189- <li><a href="index.html">Home</a></li>190- <li><a href="guide.html">Guide</a></li>191- <li><a href="screencasts.html">Screencasts</a></li>192- <li><a href="applications.html">Applications</a></li>193- </ul>194-<h2>Synopsis</h2>195-196-<pre><code>express [options] [PATH]197-</code></pre>198-199-<h2>Description</h2>200-201-<p>The <em>express</em> executable generates apps at the given <strong>PATH</strong> or the202-current working directory. Although Express is not bound to a specific203-application structure, this executable creates a maintainable base app.</p>204-205-<h2>Options</h2>206-207-<pre><code> -s, --sessions Add session support208- -t, --template ENGINE Add template ENGINE support (jade|ejs). Defaults to jade209- -c, --css ENGINE Add stylesheet ENGINE support (less|sass|stylus). Defaults to plain css210- -v, --version Output framework version211- -h, --help Output help information212-</code></pre>213- </div>214- </div>215- </body>216-</html>deleted:docs/executable.md-21 removed
123456789101112131415161718192021
## Synopsis
express [options] [PATH]
## Description
The _express_ executable generates apps at the given **PATH** or the current working directory. Although Express is not bound to a specificapplication structure, this executable creates a maintainable base app.
## Options
-s, --sessions Add session support -t, --template ENGINE Add template ENGINE support (jade|ejs). Defaults to jade -c, --css ENGINE Add stylesheet ENGINE support (less|sass|stylus). Defaults to plain css -v, --version Output framework version -h, --help Output help information
deleted:docs/guide.html-1807 removed
1-<html>2- <head>3- <title>Express - node web framework</title>4- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">5- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>6- <style>7- #tagline {8- margin-left: 75px;9- margin-bottom: 30px;10- color: rgba(255,255,255,0.7); }11- html {12- background: #1c1c1c url(images/bg.tile.jpg); }13-14- body {15- margin: 0;16- padding-bottom: 30px;17- font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";18- font-size: 14px;19- line-height: 1.5;20- -webkit-font-smoothing: antialiased;21- background: url(images/bg.jpg) 50% 0 no-repeat;22- color: #8b8b8b; }23-24- * {25- outline: none; }26-27- em {28- color: white; }29-30- a img {31- border: none !important; }32-33- a {34- font-weight: bold;35- text-decoration: none;36- color: white;37- -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;38- -webkit-transition-duration: 0.15s;39- -webkit-transition-timing-function: ease-out; }40- a:hover {41- opacity: 0.8; }42-43- h1, h2, h3, h4 {44- margin: 45px 0 0 0;45- color: white;46- text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }47-48- h3 {49- font-size: 18px; }50- h4 {51- margin-left: 10px;52- font-size: 14px;53- }54-55- pre {56- margin: 20px 10px;57- padding: 25px 20px;58- background: rgba(0,0,0,0.5);59- border: 1px solid #323232;60- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);62- -webkit-border-radius: 5px;63- -moz-border-radius: 5px; }64-65- code {66- font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }67-68- ul {69- margin: 15px 0;70- padding: 0 0 0 35px; }71- ul li {72- margin: 0;73- padding: 2px 0;74- list-style: square; }75- ul li ul {76- margin: 0;77- padding-left: 12px;78- }79-80- .man-name, #Express { display:none; }81-82- .sect {83- margin-left: 40px; }84- img {85- margin-left: 20px;86- margin-bottom: 15px;87- }88-89- #logo {90- display: block;91- margin-left: 30%;92- margin-bottom: 30px;93- width: 194px;94- height: 51px;95- background: url(images/logo.png) 0 0 no-repeat;96- text-indent: -99999px; }97- #logo:hover {98- opacity: 0.7; }99- #logo:active {100- opacity: 0.3; }101-102- #ribbon {103- position: fixed;104- top: 0;105- right: 0;106- z-index: 2; }107-108- #wrapper {109- width: 100%;110- min-height: 800px;111- background: url(images/top.png) 0 0 repeat-x; }112-113- #container {114- margin: 0 auto;115- padding-top: 80px;116- width: 550px; }117-118- #toc {119- position: fixed;120- top: 0;121- left: 0;122- margin: 0 0 0 15px;123- padding: 15px;124- height: 100%;125- background: rgba(0,0,0,0.2);126- overflow: auto;127- border-right: 1px solid rgba(255,255,255,0.05);128- }129- #toc li {130- padding: 0;131- list-style: none;132- }133- #toc li a {134- font-size: 11px;135- }136- #menu {137- margin-left: 75px;138- padding: 0;139- padding-bottom: 30px; }140- #menu li {141- display: inline;142- list-style: none; }143- #menu li a {144- display: block;145- float: left;146- margin: 0 2px;147- padding: 3px 15px;148- background: rgba(0,0,0,0.2);149- -webkit-border-radius: 8px;150- -moz-border-radius: 8px;151- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);153- -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;154- -webkit-transition-duration: 0.15s;155- -webkit-transition-timing-function: ease-out; }156- #menu li a:hover,157- #menu li a.active {158- background: rgba(0,0,0,0.5); }159- #menu li a:active {160- background: rgba(0,0,0,0.1);161- -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);162- -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }163- </style>164- <script>165- $(function(){166- $('.section').hide();167- $('.toggle, a.section-title').toggle(function(){168- $(this).siblings('ul').fadeIn(300);169- return false;170- }, function(){171- $(this).siblings('ul').fadeOut(300);172- return false;173- });174- });175- </script>176- </head>177- <body>178- <a href='http://github.com/visionmedia/express'> 179- <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 180- </a>181- <div id="wrapper">182- <div id="container"><ul id="toc">183-184-</ul><ul id="toc">185-<li><a href="#installation">Installation</a></li>186-<li><a href="#creating-a server">Creating A Server</a></li>187-<li><a href="#creating-an https server">Creating An HTTPS Server</a></li>188-<li><a href="#configuration">Configuration</a></li>189-<li><a href="#settings">Settings</a></li>190-<li><a href="#routing">Routing</a></li>191-<li><a href="#passing-route control">Passing Route Control</a></li>192-<li><a href="#middleware">Middleware</a></li>193-<li><a href="#route-middleware">Route Middleware</a></li>194-<li><a href="#http-methods">HTTP Methods</a></li>195-<li><a href="#error-handling">Error Handling</a></li>196-<li><a href="#route-param pre-conditions">Route Param Pre-conditions</a></li>197-<li><a href="#view-rendering">View Rendering</a></li>198-<li><a href="#view-partials">View Partials</a></li>199-<li><a href="#view-lookup">View Lookup</a></li>200-<li><a href="#template-engines">Template Engines</a></li>201-<li><a href="#session-support">Session Support</a></li>202-<li><a href="#migration-guide">Migration Guide</a></li>203-<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Request</a><ul class="section" id="section-Request">204-<li><a href="#req.header()">header()</a></li>205-<li><a href="#req.accepts()">accepts()</a></li>206-<li><a href="#req.is()">is()</a></li>207-<li><a href="#req.param()">param()</a></li>208-<li><a href="#req.get()">get()</a></li>209-<li><a href="#req.flash()">flash()</a></li>210-<li><a href="#req.isxmlhttprequest">isXMLHttpRequest</a></li>211-</ul></li>212-<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Response</a><ul class="section" id="section-Response">213-<li><a href="#res.header()">header()</a></li>214-<li><a href="#res.charset">charset</a></li>215-<li><a href="#res.contenttype()">contentType()</a></li>216-<li><a href="#res.attachment()">attachment()</a></li>217-<li><a href="#res.sendfile()">sendfile()</a></li>218-<li><a href="#res.download()">download()</a></li>219-<li><a href="#res.send()">send()</a></li>220-<li><a href="#res.redirect()">redirect()</a></li>221-<li><a href="#res.cookie()">cookie()</a></li>222-<li><a href="#res.clearcookie()">clearCookie()</a></li>223-<li><a href="#res.render()">render()</a></li>224-<li><a href="#res.partial()">partial()</a></li>225-<li><a href="#res.local()">local()</a></li>226-<li><a href="#res.locals()">locals()</a></li>227-</ul></li>228-<li><a href="#" class="toggle">+</a> <a class="section-title" href="#">Server</a><ul class="section" id="section-Server">229-<li><a href="#app.set()">set()</a></li>230-<li><a href="#app.enable()">enable()</a></li>231-<li><a href="#app.enabled()">enabled()</a></li>232-<li><a href="#app.disable()">disable()</a></li>233-<li><a href="#app.disabled()">disabled()</a></li>234-<li><a href="#app.configure()">configure()</a></li>235-<li><a href="#app.redirect()">redirect()</a></li>236-<li><a href="#app.error()">error()</a></li>237-<li><a href="#app.helpers()">helpers()</a></li>238-<li><a href="#app.dynamichelpers()">dynamicHelpers()</a></li>239-<li><a href="#app.lookup">lookup</a></li>240-<li><a href="#app.match">match</a></li>241-<li><a href="#app.mounted()">mounted()</a></li>242-<li><a href="#app.register()">register()</a></li>243-<li><a href="#app.listen()">listen()</a></li>244-</ul></li>245-</ul>246- <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 247- <p id="tagline">248- High performance, high class web development for249- <a href="http://nodejs.org">Node.js</a>250- </p>251- <ul id="menu">252- <li><a href="index.html">Home</a></li>253- <li><a href="guide.html">Guide</a></li>254- <li><a href="screencasts.html">Screencasts</a></li>255- <li><a href="applications.html">Applications</a></li>256- </ul>257-<h3 id="installation">Installation</h3>258-259-<pre><code>$ npm install express260-</code></pre>261-262-<p>or to access the <code>express(1)</code> executable install globally:</p>263-264-<pre><code>$ npm install -g express265-</code></pre>266-267-<h2>Quick Start</h2>268-269-<p> The quickest way to get started with express is to utilize the executable <code>express(1)</code> to generate an application as shown below:</p>270-271-<p> Create the app:</p>272-273-<pre><code>$ npm install -g express274-$ express /tmp/foo && cd /tmp/foo275-</code></pre>276-277-<p> Install dependencies:</p>278-279-<pre><code>$ npm install -d280-</code></pre>281-282-<p> Start the server:</p>283-284-<pre><code>$ node app.js285-</code></pre>286-287-<h3 id="creating-a server">Creating A Server</h3>288-289-<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>290-291-<pre><code>var app = require('express').createServer();292-293-app.get('/', function(req, res){294- res.send('hello world');295-});296-297-app.listen(3000);298-</code></pre>299-300-<h3 id="creating-an https server">Creating An HTTPS Server</h3>301-302-<p> To initialize a <em>express.HTTPSServer</em> we do the same as above, however we303- 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>304-305-<pre><code> var app = require('express').createServer({ key: ... });306-</code></pre>307-308-<h3 id="configuration">Configuration</h3>309-310-<p>Express supports arbitrary environments, such as <em>production</em> and <em>development</em>. Developers311-can use the <em>configure()</em> method to setup needs required by the current environment. When312-<em>configure()</em> is called without an environment name it will be run in <em>every</em> environment313-prior to the environment specific callback.</p>314-315-<p>In the example below we only <em>dumpExceptions</em>, and respond with exception stack traces316-in <em>development</em> mode, however for both environments we utilize <em>methodOverride</em> and <em>bodyParser</em>.317-Note the use of <em>app.router</em>, which can (optionally) be used to mount the application routes,318-otherwise the first call to <em>app.get()</em>, <em>app.post()</em>, etc will mount the routes.</p>319-320-<pre><code>app.configure(function(){321- app.use(express.methodOverride());322- app.use(express.bodyParser());323- app.use(app.router);324-});325-326-app.configure('development', function(){327- app.use(express.static(__dirname + '/public'));328- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));329-});330-331-app.configure('production', function(){332- var oneYear = 31557600000;333- app.use(express.static(__dirname + '/public', { maxAge: oneYear }));334- app.use(express.errorHandler());335-});336-</code></pre>337-338-<p>For internal and arbitrary settings Express provides the <em>set(key[, val])</em>, <em>enable(key)</em>, <em>disable(key)</em> methods:</p>339-340-<pre><code> app.configure(function(){341- app.set('views', __dirname + '/views');342- app.set('views');343- // => "/absolute/path/to/views"344-345- app.enable('some feature');346- // same as app.set('some feature', true);347-348- app.disable('some feature');349- // same as app.set('some feature', false);350-351- app.enabled('some feature')352- // => false353- });354-</code></pre>355-356-<p>To alter the environment we can set the <em>NODE_ENV</em> environment variable, for example:</p>357-358-<pre><code>$ NODE_ENV=production node app.js359-</code></pre>360-361-<p>This is <em>very</em> important, as many caching mechanisms are <em>only enabled</em> when in production.</p>362-363-<h3 id="settings">Settings</h3>364-365-<p>Express supports the following settings out of the box:</p>366-367-<ul>368-<li><em>home</em> Application base path used for <em>res.redirect()</em> and transparently handling mounted apps.</li>369-<li><em>views</em> Root views directory defaulting to <strong>CWD/views</strong></li>370-<li><em>view engine</em> Default view engine name for views rendered without extensions</li>371-<li><em>view options</em> An object specifying global view options</li>372-<li><em>view cache</em> Enable view caching (enabled in production)</li>373-<li><em>case sensitive routes</em> Enable case-sensitive routing</li>374-</ul>375-376-377-<h3 id="routing">Routing</h3>378-379-<p>Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.380-For example we may want to render a user’s account for the path <em>/user/12</em>, this381-can be done by defining the route below. The values associated to the named placeholders382-are available as <code>req.params</code>.</p>383-384-<pre><code>app.get('/user/:id', function(req, res){385- res.send('user ' + req.params.id);386-});387-</code></pre>388-389-<p>A route is simple a string which is compiled to a <em>RegExp</em> internally. For example390-when <em>/user/:id</em> is compiled, a simplified version of the regexp may look similar to:</p>391-392-<pre><code>\/user\/([^\/]+)\/?393-</code></pre>394-395-<p>Regular expression literals may also be passed for complex uses. Since capture396-groups with literal <em>RegExp</em>’s are anonymous we can access them directly <code>req.params</code>. So our first capture group would be <em>req.params[0]</em> and the second would follow as <em>req.params[1]</em>.</p>397-398-<pre><code>app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, function(req, res){399- res.send(req.params);400-});401-</code></pre>402-403-<p>Curl requests against the previously defined route:</p>404-405-<pre><code> $ curl http://dev:3000/user406- [null,null]407- $ curl http://dev:3000/users408- [null,null]409- $ curl http://dev:3000/users/1410- ["1",null]411- $ curl http://dev:3000/users/1..15412- ["1","15"]413-</code></pre>414-415-<p>Below are some route examples, and the associated paths that they416-may consume:</p>417-418-<pre><code> "/user/:id"419- /user/12420-421- "/users/:id?"422- /users/5423- /users424-425- "/files/*"426- /files/jquery.js427- /files/javascripts/jquery.js428-429- "/file/*.*"430- /files/jquery.js431- /files/javascripts/jquery.js432-433- "/user/:id/:operation?"434- /user/1435- /user/1/edit436-437- "/products.:format"438- /products.json439- /products.xml440-441- "/products.:format?"442- /products.json443- /products.xml444- /products445-446- "/user/:id.:format?"447- /user/12448- /user/12.json449-</code></pre>450-451-<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>452-453-<pre><code>var express = require('express')454- , app = express.createServer();455-456-app.use(express.bodyParser());457-458-app.post('/', function(req, res){459- res.send(req.body);460-});461-462-app.listen(3000);463-</code></pre>464-465-<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([0-9]+)’</em> which will <em>not</em> match unless the placeholder value contains only digits.</p>466-467-<h3 id="passing-route control">Passing Route Control</h3>468-469-<p>We may pass control to the next <em>matching</em> route, by calling the <em>third</em> argument,470-the <em>next()</em> function. When a match cannot be made, control is passed back to Connect,471-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>472-473-<pre><code>app.get('/users/:id?', function(req, res, next){474- var id = req.params.id;475- if (id) {476- // do something477- } else {478- next();479- }480-});481-482-app.get('/users', function(req, res){483- // do something else484-});485-</code></pre>486-487-<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>488-489-<pre><code>var express = require('express')490- , app = express.createServer();491-492-var users = [{ name: 'tj' }];493-494-app.all('/user/:id/:op?', function(req, res, next){495- req.user = users[req.params.id];496- if (req.user) {497- next();498- } else {499- next(new Error('cannot find user ' + req.params.id));500- }501-});502-503-app.get('/user/:id', function(req, res){504- res.send('viewing ' + req.user.name);505-});506-507-app.get('/user/:id/edit', function(req, res){508- res.send('editing ' + req.user.name);509-});510-511-app.put('/user/:id', function(req, res){512- res.send('updating ' + req.user.name);513-});514-515-app.get('*', function(req, res){516- res.send('what???', 404);517-});518-519-app.listen(3000); 520-</code></pre>521-522-<h3 id="middleware">Middleware</h3>523-524-<p>Middleware via <a href="http://github.com/senchalabs/connect">Connect</a> can be525-passed to <em>express.createServer()</em> as you would with a regular Connect server. For example:</p>526-527-<pre><code> var express = require('express');528-529-var app = express.createServer(530- express.logger()531- , express.bodyParser()532- );533-</code></pre>534-535-<p>Alternatively we can <em>use()</em> them which is useful when adding middleware within <em>configure()</em> blocks, in a progressive manner.</p>536-537-<pre><code>app.use(express.logger({ format: ':method :url' }));538-</code></pre>539-540-<p>Typically with connect middleware you would <em>require(‘connect’)</em> like so:</p>541-542-<pre><code>var connect = require('connect');543-app.use(connect.logger());544-app.use(connect.bodyParser());545-</code></pre>546-547-<p>This is somewhat annoying, so express re-exports these middleware properties, however they are <em>identical</em>:</p>548-549-<pre><code>app.use(express.logger());550-app.use(express.bodyParser());551-</code></pre>552-553-<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’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>554-555-<p>Many Express applications may contain the line <em>app.use(app.router)</em>, while this may appear strange, it’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>556-557-<pre><code>app.use(express.logger(...));558-app.use(express.bodyParser(...));559-app.use(express.cookieParser(...));560-app.use(express.session(...));561-app.use(app.router);562-app.use(express.static(...));563-app.use(express.errorHandler(...));564-</code></pre>565-566-<p>First we add <em>logger()</em> so that it may wrap node’s <em>req.end()</em> method, providing us with response-time data. Next the request’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>567-568-<pre><code>var downloads = {};569-570-app.use(app.router);571-app.use(express.static(__dirname + '/public'));572-573-app.get('/*', function(req, res, next){574- var file = req.params[0];575- downloads[file] = downloads[file] || 0;576- downloads[file]++;577- next();578-});579-</code></pre>580-581-<h3 id="route-middleware">Route Middleware</h3>582-583-<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>584-585-<p>Typically async data retrieval might look similar to below, where we take the <em>:id</em> parameter, and attempt loading a user.</p>586-587-<pre><code>app.get('/user/:id', function(req, res, next){588- loadUser(req.params.id, function(err, user){589- if (err) return next(err);590- res.send('Viewing user ' + user.name);591- });592-});593-</code></pre>594-595-<p>To keep things DRY and to increase readability we can apply this logic within a middleware. As you can see below, abstracting this logic into middleware allows us to reuse it, and clean up our route at the same time.</p>596-597-<pre><code>function loadUser(req, res, next) {598- // You would fetch your user from the db599- var user = users[req.params.id];600- if (user) {601- req.user = user;602- next();603- } else {604- next(new Error('Failed to load user ' + req.params.id));605- }606-}607-608-app.get('/user/:id', loadUser, function(req, res){609- res.send('Viewing user ' + req.user.name);610-});611-</code></pre>612-613-<p>Multiple route middleware can be applied, and will be executed sequentially to apply further logic such as restricting access to a user account. In the example below only the authenticated user may edit his/her account.</p>614-615-<pre><code>function andRestrictToSelf(req, res, next) {616- req.authenticatedUser.id == req.user.id617- ? next()618- : next(new Error('Unauthorized'));619-}620-621-app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){622- res.send('Editing user ' + req.user.name);623-});624-</code></pre>625-626-<p>Keeping in mind that middleware are simply functions, we can define function that <em>returns</em> the middleware in order to create a more expressive and flexible solution as shown below.</p>627-628-<pre><code>function andRestrictTo(role) {629- return function(req, res, next) {630- req.authenticatedUser.role == role631- ? next()632- : next(new Error('Unauthorized'));633- }634-}635-636-app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){637- res.send('Deleted user ' + req.user.name);638-});639-</code></pre>640-641-<p>Commonly used “stacks” of middleware can be passed as an array (<em>applied recursively</em>), which can be mixed and matched to any degree.</p>642-643-<pre><code>var a = [middleware1, middleware2]644- , b = [middleware3, middleware4]645- , all = [a, b];646-647-app.get('/foo', a, function(){});648-app.get('/bar', a, function(){});649-650-app.get('/', a, middleware3, middleware4, function(){});651-app.get('/', a, b, function(){});652-app.get('/', all, function(){});653-</code></pre>654-655-<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>656-657-<p>There are times when we may want to “skip” passed remaining route middleware, but continue matching subsequent routes. To do this we invoke <code>next()</code> with the string “route” <code>next('route')</code>. If no remaining routes match the request url then Express will respond with 404 Not Found.</p>658-659-<h3 id="http-methods">HTTP Methods</h3>660-661-<p>We have seen <em>app.get()</em> a few times, however Express also exposes other familiar HTTP verbs in the same manner, such as <em>app.post()</em>, <em>app.del()</em>, etc.</p>662-663-<p> A common example for <em>POST</em> usage, is when “submitting” a form. Below we simply set our form method to “post” in our html, and control will be given to the route we have defined below it.</p>664-665-<pre><code> <form method="post" action="/">666- <input type="text" name="user[name]" />667- <input type="text" name="user[email]" />668- <input type="submit" value="Submit" />669- </form>670-</code></pre>671-672-<p>By default Express does not know what to do with this request body, so we should add the <em>bodyParser</em> middleware, which will parse <em>application/x-www-form-urlencoded</em> and <em>application/json</em> request bodies and place the variables in <em>req.body</em>. We can do this by “using” the middleware as shown below:</p>673-674-<pre><code>app.use(express.bodyParser());675-</code></pre>676-677-<p>Our route below will now have access to the <em>req.body.user</em> object which will contain the <em>name</em> and <em>email</em> properties when defined.</p>678-679-<pre><code>app.post('/', function(req, res){680- console.log(req.body.user);681- res.redirect('back');682-});683-</code></pre>684-685-<p>When using methods such as <em>PUT</em> with a form, we can utilize a hidden input named <em>_method</em>, which can be used to alter the HTTP method. To do so we first need the <em>methodOverride</em> middleware, which should be placed below <em>bodyParser</em> so that it can utilize it’s <em>req.body</em> containing the form values.</p>686-687-<pre><code>app.use(express.bodyParser());688-app.use(express.methodOverride());689-</code></pre>690-691-<p>The reason that these are not always defaults, is simply because these are not required for Express to be fully functional. Depending on the needs of your application, you may not need these at all, your methods such as <em>PUT</em> and <em>DELETE</em> can still be accessed by clients which can use them directly, although <em>methodOverride</em> provides a great solution for forms. Below shows what the usage of <em>PUT</em> might look like:</p>692-693-<pre><code><form method="post" action="/">694- <input type="hidden" name="_method" value="put" />695- <input type="text" name="user[name]" />696- <input type="text" name="user[email]" />697- <input type="submit" value="Submit" /> 698-</form>699-700-app.put('/', function(){701- console.log(req.body.user);702- res.redirect('back');703-});704-</code></pre>705-706-<h3 id="error-handling">Error Handling</h3>707-708-<p>Express provides the <em>app.error()</em> method which receives exceptions thrown within a route,709-or passed to <em>next(err)</em>. Below is an example which serves different pages based on our710-ad-hoc <em>NotFound</em> exception:</p>711-712-<pre><code>function NotFound(msg){713- this.name = 'NotFound';714- Error.call(this, msg);715- Error.captureStackTrace(this, arguments.callee);716-}717-718-NotFound.prototype.__proto__ = Error.prototype;719-720-app.get('/404', function(req, res){721- throw new NotFound;722-});723-724-app.get('/500', function(req, res){725- throw new Error('keyboard cat!');726-});727-</code></pre>728-729-<p>We can call <em>app.error()</em> several times as shown below.730-Here we check for an instanceof <em>NotFound</em> and show the731-404 page, or we pass on to the next error handler.</p>732-733-<p>Note that these handlers can be defined anywhere, as they734-will be placed below the route handlers on <em>listen()</em>. This735-allows for definition within <em>configure()</em> blocks so we can736-handle exceptions in different ways based on the environment.</p>737-738-<pre><code>app.error(function(err, req, res, next){739- if (err instanceof NotFound) {740- res.render('404.jade');741- } else {742- next(err);743- }744-});745-</code></pre>746-747-<p>Here we assume all errors as 500 for the simplicity of748-this demo, however you can choose whatever you like. For example when node performs filesystem syscalls, you may receive an error object with the <em>error.code</em> of <em>ENOENT</em>, meaning “no such file or directory”, we can utilize this in our error handling and display a page specific to this if desired.</p>749-750-<pre><code>app.error(function(err, req, res){751- res.render('500.jade', {752- error: err753- });754-});755-</code></pre>756-757-<p>Our apps could also utilize the Connect <em>errorHandler</em> middleware758-to report on exceptions. For example if we wish to output exceptions759-in “development” mode to <em>stderr</em> we can use:</p>760-761-<pre><code>app.use(express.errorHandler({ dumpExceptions: true }));762-</code></pre>763-764-<p>Also during development we may want fancy html pages to show exceptions765-that are passed or thrown, so we can set <em>showStack</em> to true:</p>766-767-<pre><code>app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));768-</code></pre>769-770-<p>The <em>errorHandler</em> middleware also responds with <em>json</em> if <em>Accept: application/json</em>771-is present, which is useful for developing apps that rely heavily on client-side JavaScript.</p>772-773-<h3 id="route-param pre-conditions">Route Param Pre-conditions</h3>774-775-<p>Route param pre-conditions can drastically improve the readability of your application, through implicit loading of data, and validation of request urls. For example if you are constantly fetching common data for several routes, such as loading a user for <em>/user/:id</em>, we might typically do something like below:</p>776-777-<pre><code>app.get('/user/:userId', function(req, res, next){778- User.get(req.params.userId, function(err, user){779- if (err) return next(err);780- res.send('user ' + user.name);781- });782-}); 783-</code></pre>784-785-<p>With preconditions our params can be mapped to callbacks which may perform validation, coercion, or even loading data from a database. Below we invoke <em>app.param()</em> with the parameter name we wish to map to some middleware, as you can see we receive the <em>id</em> argument which contains the placeholder value. Using this we load the user and perform error handling as usual, and simple call <em>next()</em> to pass control to the next precondition or route handler.</p>786-787-<pre><code>app.param('userId', function(req, res, next, id){788- User.get(id, function(err, user){789- if (err) return next(err);790- if (!user) return next(new Error('failed to find user'));791- req.user = user;792- next();793- });794-});795-</code></pre>796-797-<p>Doing so, as mentioned drastically improves our route readability, and allows us to easily share this logic throughout our application:</p>798-799-<pre><code>app.get('/user/:userId', function(req, res){800- res.send('user ' + req.user.name);801-});802-</code></pre>803-804-<h3 id="view-rendering">View Rendering</h3>805-806-<p>View filenames take the form “<name>.<engine>”, where <engine> is the name807-of the module that will be required. For example the view <em>layout.ejs</em> will808-tell the view system to <em>require(‘ejs’)</em>, the module being loaded must export the method <em>exports.compile(str, options)</em>, and return a <em>Function</em> to comply with Express. To alter this behaviour809-<em>app.register()</em> can be used to map engines to file extensions, so that for example “foo.html” can be rendered by ejs.</p>810-811-<p>Below is an example using <a href="http://github.com/visionmedia/jade">Jade</a> to render <em>index.html</em>,812-and since we do not use <em>layout: false</em> the rendered contents of <em>index.jade</em> will be passed as813-the <em>body</em> local variable in <em>layout.jade</em>.</p>814-815-<pre><code>app.get('/', function(req, res){816- res.render('index.jade', { title: 'My Site' });817-});818-</code></pre>819-820-<p>The new <em>view engine</em> setting allows us to specify our default template engine,821-so for example when using jade we could set:</p>822-823-<pre><code>app.set('view engine', 'jade');824-</code></pre>825-826-<p>Allowing us to render with:</p>827-828-<pre><code>res.render('index');829-</code></pre>830-831-<p>vs:</p>832-833-<pre><code>res.render('index.jade');834-</code></pre>835-836-<p>When <em>view engine</em> is set, extensions are entirely optional, however we can still837-mix and match template engines:</p>838-839-<pre><code>res.render('another-page.ejs');840-</code></pre>841-842-<p>Express also provides the <em>view options</em> setting, which is applied each time a view is rendered, so for example if you rarely use layouts you may set:</p>843-844-<pre><code>app.set('view options', {845- layout: false846-});847-</code></pre>848-849-<p>Which can then be overridden within the <em>res.render()</em> call if need be:</p>850-851-<pre><code>res.render('myview.ejs', { layout: true });852-</code></pre>853-854-<p>When an alternate layout is required, we may also specify a path. For example if we have <em>view engine</em> set to <em>jade</em> and a file named <em>./views/mylayout.jade</em> we can simply pass:</p>855-856-<pre><code>res.render('page', { layout: 'mylayout' });857-</code></pre>858-859-<p>Otherwise we must specify the extension:</p>860-861-<pre><code>res.render('page', { layout: 'mylayout.jade' });862-</code></pre>863-864-<p>These paths may also be absolute:</p>865-866-<pre><code>res.render('page', { layout: __dirname + '/../../mylayout.jade' });867-</code></pre>868-869-<p>A good example of this is specifying custom <em>ejs</em> opening and closing tags:</p>870-871-<pre><code>app.set('view options', {872- open: '{{',873- close: '}}'874-});875-</code></pre>876-877-<h3 id="view-partials">View Partials</h3>878-879-<p>The Express view system has built-in support for partials and collections, which are “mini” views representing a document fragment. For example rather than iterating880-in a view to display comments, we could use partial collection:</p>881-882-<pre><code>partial('comment', { collection: comments });883-</code></pre>884-885-<p>If no other options or local variables are desired, we can omit the object and simply pass our array, which is equivalent to above:</p>886-887-<pre><code>partial('comment', comments);888-</code></pre>889-890-<p>When using the partial collection support a few “magic” locals are provided891-for free:</p>892-893-<ul>894-<li><em>firstInCollection</em> true if this is the first object</li>895-<li><em>indexInCollection</em> index of the object in the collection</li>896-<li><em>lastInCollection</em> true if this is the last object</li>897-<li><em>collectionLength</em> length of the collection</li>898-</ul>899-900-901-<p>Local variables passed (or generated) take precedence, however locals passed to the parent view are available in the child view as well. So for example if we were to render a blog post with <em>partial(‘blog/post’, post)</em> it would generate the <em>post</em> local, but the view calling this function had the local <em>user</em>, it would be available to the <em>blog/post</em> view as well.</p>902-903-<p>For documentation on altering the object name view <a href="http://expressjs.com/guide.html#res-partial-view-options-">res.partial()</a>.</p>904-905-<p><strong>NOTE:</strong> be careful about when you use partial collections, as rendering an array with a length of 100 means we have to render 100 views. For simple collections you may inline the iteration instead of using partial collection support to decrease overhead.</p>906-907-<h3 id="view-lookup">View Lookup</h3>908-909-<p>View lookup is performed relative to the parent view, for example if we had a page view named <em>views/user/list.jade</em>, and within that view we did <em>partial(‘edit’)</em> it would attempt to load <em>views/user/edit.jade</em>, whereas <em>partial(‘../messages’)</em> would load <em>views/messages.jade</em>.</p>910-911-<p>The view system also allows for index templates, allowing you to have a directory of the same name. For example within a route we may have <em>res.render(‘users’)</em> either <em>views/users.jade</em>, or <em>views/users/index.jade</em>.</p>912-913-<p>When utilizing index views as shown above, we may reference <em>views/users/index.jade</em> from a view in the same directory by <em>partial(‘users’)</em>, and the view system will try <em>../users/index</em>, preventing us from needing to call <em>partial(‘index’)</em>.</p>914-915-<h3 id="template-engines">Template Engines</h3>916-917-<p>Below are a few template engines commonly used with Express:</p>918-919-<ul>920-<li><a href="http://github.com/visionmedia/haml.js">Haml</a> haml implementation</li>921-<li><a href="http://jade-lang.com">Jade</a> haml.js successor</li>922-<li><a href="http://github.com/visionmedia/ejs">EJS</a> Embedded JavaScript</li>923-<li><a href="http://github.com/mauricemach/coffeekup">CoffeeKup</a> CoffeeScript based templating</li>924-<li><a href="https://github.com/kof/node-jqtpl">jQuery Templates</a> for node</li>925-</ul>926-927-928-<h3 id="session-support">Session Support</h3>929-930-<p>Sessions support can be added by using Connect’s <em>session</em> middleware. To do so we also need the <em>cookieParser</em> middleware place above it, which will parse and populate cookie data to <em>req.cookies</em>.</p>931-932-<pre><code>app.use(express.cookieParser());933-app.use(express.session({ secret: "keyboard cat" }));934-</code></pre>935-936-<p>By default the <em>session</em> middleware uses the memory store bundled with Connect, however many implementations exist. For example <a href="http://github.com/visionmedia/connect-redis">connect-redis</a> supplies a <a href="http://code.google.com/p/redis/">Redis</a> session store and can be used as shown below:</p>937-938-<pre><code>var RedisStore = require('connect-redis')(express);939-app.use(express.cookieParser());940-app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));941-</code></pre>942-943-<p>Now the <em>req.session</em> and <em>req.sessionStore</em> properties will be accessible to all routes and subsequent middleware. Properties on <em>req.session</em> are automatically saved on a response, so for example if we wish to shopping cart data:</p>944-945-<pre><code>var RedisStore = require('connect-redis')(express);946-app.use(express.bodyParser());947-app.use(express.cookieParser());948-app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));949-950-app.post('/add-to-cart', function(req, res){951- // Perhaps we posted several items with a form952- // (use the bodyParser() middleware for this)953- var items = req.body.items;954- req.session.items = items;955- res.redirect('back');956-});957-958-app.get('/add-to-cart', function(req, res){959- // When redirected back to GET /add-to-cart960- // we could check req.session.items && req.session.items.length961- // to print out a message962- if (req.session.items && req.session.items.length) {963- req.flash('info', 'You have %s items in your cart', req.session.items.length);964- }965- res.render('shopping-cart');966-});967-</code></pre>968-969-<p>The <em>req.session</em> object also has methods such as <em>Session#touch()</em>, <em>Session#destroy()</em>, <em>Session#regenerate()</em> among others to maintain and manipulate sessions. For more information view the <a href="http://senchalabs.github.com/connect/middleware-session.html">Connect Session</a> documentation.</p>970-971-<h3 id="migration-guide">Migration Guide</h3>972-973-<p> Express 1.x developers may reference the <a href="migrate.html">Migration Guide</a> to get up to speed on how to upgrade your application to work with Express 2.x, Connect 1.x, and Node 0.4.x.</p>974-975-<h3 id="req.header()">req.header(key[, defaultValue])</h3>976-977-<p>Get the case-insensitive request header <em>key</em>, with optional <em>defaultValue</em>:</p>978-979-<pre><code>req.header('Host');980-req.header('host');981-req.header('Accept', '*/*');982-</code></pre>983-984-<p>The <em>Referrer</em> and <em>Referer</em> header fields are special-cased, either will work:</p>985-986-<pre><code>// sent Referrer: http://google.com987-988-req.header('Referer');989-// => "http://google.com"990-991-req.header('Referrer');992-// => "http://google.com"993-</code></pre>994-995-<h3 id="req.accepts()">req.accepts(type)</h3>996-997-<p>Check if the <em>Accept</em> header is present, and includes the given <em>type</em>.</p>998-999-<p>When the <em>Accept</em> header is not present <em>true</em> is returned. Otherwise1000-the given <em>type</em> is matched by an exact match, and then subtypes. You1001-may pass the subtype such as “html” which is then converted internally1002-to “text/html” using the mime lookup table.</p>1003-1004-<pre><code>// Accept: text/html1005-req.accepts('html');1006-// => true1007-1008-// Accept: text/*; application/json1009-req.accepts('html');1010-req.accepts('text/html');1011-req.accepts('text/plain');1012-req.accepts('application/json');1013-// => true1014-1015-req.accepts('image/png');1016-req.accepts('png');1017-// => false1018-</code></pre>1019-1020-<h3 id="req.is()">req.is(type)</h3>1021-1022-<p>Check if the incoming request contains the <em>Content-Type</em>1023-header field, and it contains the give mime <em>type</em>.</p>1024-1025-<pre><code> // With Content-Type: text/html; charset=utf-81026- req.is('html');1027- req.is('text/html');1028- // => true1029-1030- // When Content-Type is application/json1031- req.is('json');1032- req.is('application/json');1033- // => true1034-1035- req.is('html');1036- // => false1037-</code></pre>1038-1039-<p>Ad-hoc callbacks can also be registered with Express, to perform1040-assertions again the request, for example if we need an expressive1041-way to check if our incoming request is an image, we can register <em>“an image”</em>1042-callback:</p>1043-1044-<pre><code> app.is('an image', function(req){1045- return 0 == req.headers['content-type'].indexOf('image');1046- });1047-</code></pre>1048-1049-<p>Now within our route callbacks, we can use to to assert content types1050-such as <em>“image/jpeg”</em>, <em>“image/png”</em>, etc.</p>1051-1052-<pre><code> app.post('/image/upload', function(req, res, next){1053- if (req.is('an image')) {1054- // do something1055- } else {1056- next();1057- }1058- });1059-</code></pre>1060-1061-<p>Keep in mind this method is <em>not</em> limited to checking <em>Content-Type</em>, you1062-can perform any request assertion you wish.</p>1063-1064-<p>Wildcard matches can also be made, simplifying our example above for <em>“an image”</em>, by asserting the <em>subtype</em> only:</p>1065-1066-<pre><code>req.is('image/*');1067-</code></pre>1068-1069-<p>We may also assert the <em>type</em> as shown below, which would return true for <em>“application/json”</em>, and <em>“text/json”</em>.</p>1070-1071-<pre><code>req.is('*/json');1072-</code></pre>1073-1074-<h3 id="req.param()">req.param(name[, default])</h3>1075-1076-<p>Return the value of param <em>name</em> when present or <em>default</em>.</p>1077-1078-<ul>1079-<li>Checks route params (<em>req.params</em>), ex: /user/:id</li>1080-<li>Checks query string params (<em>req.query</em>), ex: ?id=12</li>1081-<li>Checks urlencoded body params (<em>req.body</em>), ex: id=12</li>1082-</ul>1083-1084-1085-<p>To utilize urlencoded request bodies, <em>req.body</em>1086-should be an object. This can be done by using1087-the _express.bodyParser middleware.</p>1088-1089-<h3 id="req.get()">req.get(field, param)</h3>1090-1091-<p> Get <em>field</em>’s <em>param</em> value, defaulting to ‘’ when the <em>param</em>1092- or <em>field</em> is not present.</p>1093-1094-<pre><code> req.get('content-disposition', 'filename');1095- // => "something.png"1096-1097- req.get('Content-Type', 'boundary');1098- // => "--foo-bar-baz"1099-</code></pre>1100-1101-<h3 id="req.flash()">req.flash(type[, msg])</h3>1102-1103-<p>Queue flash <em>msg</em> of the given <em>type</em>.</p>1104-1105-<pre><code>req.flash('info', 'email sent');1106-req.flash('error', 'email delivery failed');1107-req.flash('info', 'email re-sent');1108-// => 21109-1110-req.flash('info');1111-// => ['email sent', 'email re-sent']1112-1113-req.flash('info');1114-// => []1115-1116-req.flash();1117-// => { error: ['email delivery failed'], info: [] }1118-</code></pre>1119-1120-<p>Flash notification message may also utilize formatters, by default only the %s string formatter is available:</p>1121-1122-<pre><code>req.flash('info', 'email delivery to _%s_ from _%s_ failed.', toUser, fromUser);1123-</code></pre>1124-1125-<h3 id="req.isxmlhttprequest">req.isXMLHttpRequest</h3>1126-1127-<p>Also aliased as <em>req.xhr</em>, this getter checks the <em>X-Requested-With</em> header1128-to see if it was issued by an <em>XMLHttpRequest</em>:</p>1129-1130-<pre><code>req.xhr1131-req.isXMLHttpRequest1132-</code></pre>1133-1134-<h3 id="res.header()">res.header(key[, val])</h3>1135-1136-<p>Get or set the response header <em>key</em>.</p>1137-1138-<pre><code>res.header('Content-Length');1139-// => undefined1140-1141-res.header('Content-Length', 123);1142-// => 1231143-1144-res.header('Content-Length');1145-// => 1231146-</code></pre>1147-1148-<h3 id="res.charset">res.charset</h3>1149-1150-<p>Sets the charset for subsequent <code>Content-Type</code> header fields. For example <code>res.send()</code> and <code>res.render()</code> default to “utf8”, so we may explicitly set the charset before rendering a template:</p>1151-1152-<pre><code>res.charset = 'ISO-8859-1';1153-res.render('users');1154-</code></pre>1155-1156-<p>or before responding with <code>res.send()</code>:</p>1157-1158-<pre><code>res.charset = 'ISO-8859-1';1159-res.send(str);1160-</code></pre>1161-1162-<p>or with node’s <code>res.end()</code>:</p>1163-1164-<pre><code>res.charset = 'ISO-8859-1';1165-res.header('Content-Type', 'text/plain');1166-res.end(str);1167-</code></pre>1168-1169-<h3 id="res.contenttype()">res.contentType(type)</h3>1170-1171-<p>Sets the <em>Content-Type</em> response header to the given <em>type</em>.</p>1172-1173-<pre><code> var filename = 'path/to/image.png';1174- res.contentType(filename);1175- // Content-Type is now "image/png"1176-</code></pre>1177-1178-<p>A literal <em>Content-Type</em> works as well:</p>1179-1180-<pre><code> res.contentType('application/json');1181-</code></pre>1182-1183-<p>Or simply the extension without leading <code>.</code>:</p>1184-1185-<pre><code> res.contentType('json');1186-</code></pre>1187-1188-<h3 id="res.attachment()">res.attachment([filename])</h3>1189-1190-<p>Sets the <em>Content-Disposition</em> response header to “attachment”, with optional <em>filename</em>.</p>1191-1192-<pre><code> res.attachment('path/to/my/image.png');1193-</code></pre>1194-1195-<h3 id="res.sendfile()">res.sendfile(path[, options[, callback]])</h3>1196-1197-<p>Used by <code>res.download()</code> to transfer an arbitrary file.</p>1198-1199-<pre><code>res.sendfile('path/to/my.file');1200-</code></pre>1201-1202-<p>This method accepts an optional callback which is called when1203-an error occurs, or when the transfer is complete. By default failures call <code>next(err)</code>, however when a callback is supplied you must do this explicitly, or act on the error.</p>1204-1205-<pre><code>res.sendfile(path, function(err){1206- if (err) {1207- next(err);1208- } else {1209- console.log('transferred %s', path);1210- }1211-});1212-</code></pre>1213-1214-<p>Options may also be passed to the internal <em>fs.createReadStream()</em> call, for example altering the <em>bufferSize</em>:</p>1215-1216-<pre><code>res.sendfile(path, { bufferSize: 1024 }, function(err){1217- // handle1218-});1219-</code></pre>1220-1221-<h3 id="res.download()">res.download(file[, filename[, callback[, callback2]]])</h3>1222-1223-<p>Transfer the given <em>file</em> as an attachment with optional alternative <em>filename</em>.</p>1224-1225-<pre><code>res.download('path/to/image.png');1226-res.download('path/to/image.png', 'foo.png');1227-</code></pre>1228-1229-<p>This is equivalent to:</p>1230-1231-<pre><code>res.attachment(file);1232-res.sendfile(file);1233-</code></pre>1234-1235-<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>1236-1237-<pre><code>res.download(path, 'expenses.doc', function(err){1238- // handle1239-});1240-</code></pre>1241-1242-<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>1243-1244-<pre><code>res.download(path, function(err){1245- // error or finished1246-}, function(err){1247- // connection related error1248-});1249-</code></pre>1250-1251-<h3 id="res.send()">res.send(body|status[, headers|status[, status]])</h3>1252-1253-<p>The <em>res.send()</em> method is a high level response utility allowing you to pass1254-objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:</p>1255-1256-<pre><code> res.send(); // 2041257- res.send(new Buffer('wahoo'));1258- res.send({ some: 'json' });1259- res.send('<p>some html</p>');1260- res.send('Sorry, cant find that', 404);1261- res.send('text', { 'Content-Type': 'text/plain' }, 201);1262- res.send(404);1263-</code></pre>1264-1265-<p>By default the <em>Content-Type</em> response header is set, however if explicitly1266-assigned through <code>res.send()</code> or previously with <code>res.header()</code> or <code>res.contentType()</code>1267-it will not be set again.</p>1268-1269-<p>Note that this method <em>end()</em>s the response, so you will want to use node’s <em>res.write()</em> for multiple writes or streaming.</p>1270-1271-<h3 id="res.redirect()">res.redirect(url[, status])</h3>1272-1273-<p>Redirect to the given <em>url</em> with a default response <em>status</em> of 302.</p>1274-1275-<pre><code>res.redirect('/', 301);1276-res.redirect('/account');1277-res.redirect('http://google.com');1278-res.redirect('home');1279-res.redirect('back');1280-</code></pre>1281-1282-<p>Express supports “redirect mapping”, which by default provides <em>home</em>, and <em>back</em>.1283-The <em>back</em> map checks the <em>Referrer</em> and <em>Referer</em> headers, while <em>home</em> utilizes1284-the “home” setting and defaults to “/”.</p>1285-1286-<h3 id="res.cookie()">res.cookie(name, val[, options])</h3>1287-1288-<p>Sets the given cookie <em>name</em> to <em>val</em>, with options <em>httpOnly</em>, <em>secure</em>, <em>expires</em> etc.</p>1289-1290-<pre><code>// "Remember me" for 15 minutes 1291-res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000), httpOnly: true });1292-</code></pre>1293-1294-<p>The <em>maxAge</em> property may be used to set <em>expires</em> relative to <em>Date.now()</em> in milliseconds, so our example above can now become:</p>1295-1296-<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });1297-</code></pre>1298-1299-<p>To parse incoming <em>Cookie</em> headers, use the <em>cookieParser</em> middleware, which provides the <em>req.cookies</em> object:</p>1300-1301-<pre><code>app.use(express.cookieParser());1302-1303-app.get('/', function(req, res){1304- // use req.cookies.rememberme1305-});1306-</code></pre>1307-1308-<h3 id="res.clearcookie()">res.clearCookie(name[, options])</h3>1309-1310-<p>Clear cookie <em>name</em> by setting “expires” far in the past.</p>1311-1312-<pre><code>res.clearCookie('rememberme');1313-</code></pre>1314-1315-<h3 id="res.render()">res.render(view[, options[, fn]])</h3>1316-1317-<p>Render <em>view</em> with the given <em>options</em> and optional callback <em>fn</em>.1318-When a callback function is given a response will <em>not</em> be made1319-automatically, however otherwise a response of <em>200</em> and <em>text/html</em> is given.</p>1320-1321-<p>The <em>options</em> passed are the local variables as well, for example if we want to expose “user” to the view, and prevent a local we do so within the same object:</p>1322-1323-<pre><code>var user = { name: 'tj' };1324-res.render('index', { layout: false, user: user });1325-</code></pre>1326-1327-<h3 id="res.partial()">res.partial(view[, options])</h3>1328-1329-<p>Render <em>view</em> partial with the given <em>options</em>. This method is always available1330-to the view as a local variable.</p>1331-1332-<ul>1333-<li><em>object</em> the object named by <em>as</em> or derived from the view name</li>1334-<li><p><em>as</em> Variable name for each <em>collection</em> or <em>object</em> value, defaults to the view name.</p>1335-1336-<ul>1337-<li>as: ‘something’ will add the <em>something</em> local variable</li>1338-<li>as: this will use the collection value as the template context</li>1339-<li>as: global will merge the collection value’s properties with <em>locals</em></li>1340-</ul>1341-</li>1342-<li><p><em>collection</em> Array of objects, the name is derived from the view name itself.1343-For example <em>video.html</em> will have a object <em>video</em> available to it.</p></li>1344-</ul>1345-1346-1347-<p>The following are equivalent, and the name of collection value when passed1348-to the partial will be <em>movie</em> as derived from the name.</p>1349-1350-<pre><code>partial('theatre/movie.jade', { collection: movies });1351-partial('theatre/movie.jade', movies);1352-partial('movie.jade', { collection: movies });1353-partial('movie.jade', movies);1354-partial('movie', movies);1355-// In view: movie.director1356-</code></pre>1357-1358-<p>To change the local from <em>movie</em> to <em>video</em> we can use the “as” option:</p>1359-1360-<pre><code>partial('movie', { collection: movies, as: 'video' });1361-// In view: video.director1362-</code></pre>1363-1364-<p>Also we can make our movie the value of <em>this</em> within our view so that instead1365-of <em>movie.director</em> we could use <em>this.director</em>.</p>1366-1367-<pre><code>partial('movie', { collection: movies, as: this });1368-// In view: this.director1369-</code></pre>1370-1371-<p>Another alternative is to “expand” the properties of the collection item into1372-pseudo globals (local variables) by using <em>as: global</em>, which again is syntactic sugar:</p>1373-1374-<pre><code>partial('movie', { collection: movies, as: global });1375-// In view: director1376-</code></pre>1377-1378-<p>This same logic applies to a single partial object usage:</p>1379-1380-<pre><code>partial('movie', { object: movie, as: this });1381-// In view: this.director1382-1383-partial('movie', { object: movie, as: global });1384-// In view: director1385-1386-partial('movie', { object: movie, as: 'video' });1387-// In view: video.director1388-1389-partial('movie', { object: movie });1390-// In view: movie.director1391-</code></pre>1392-1393-<p>When a non-collection (does <em>not</em> have <em>.length</em>) is passed as the second argument, it is assumed to be the <em>object</em>, after which the object’s local variable name is derived from the view name:</p>1394-1395-<pre><code>var movie = new Movie('Nightmare Before Christmas', 'Tim Burton')1396-partial('movie', movie)1397-// => In view: movie.director1398-</code></pre>1399-1400-<p>The exception of this, is when a “plain” object, aka “{}” or “new Object” is passed, which is considered an object with local variable. For example some may expect a “movie” local with the following, however since it is a plain object “director” and “title” are simply locals:</p>1401-1402-<pre><code>var movie = { title: 'Nightmare Before Christmas', director: 'Tim Burton' }; 1403-partial('movie', movie)1404-</code></pre>1405-1406-<p>For cases like this where passing a plain object is desired, simply assign it to a key, or use the <code>object</code> key which will use the filename-derived variable name. The examples below are equivalent:</p>1407-1408-<pre><code> partial('movie', { locals: { movie: movie }})1409- partial('movie', { movie: movie })1410- partial('movie', { object: movie })1411-</code></pre>1412-1413-<p>This exact API can be utilized from within a route, to respond with a fragment via Ajax or WebSockets, for example we can render a collection of users directly from a route:</p>1414-1415-<pre><code>app.get('/users', function(req, res){1416- if (req.xhr) {1417- // respond with the each user in the collection1418- // passed to the "user" view1419- res.partial('user', users);1420- } else {1421- // respond with layout, and users page1422- // which internally does partial('user', users)1423- // along with other UI1424- res.render('users', { users: users });1425- }1426-});1427-</code></pre>1428-1429-<h3 id="res.local()">res.local(name[, val])</h3>1430-1431-<p>Get or set the given local variable <em>name</em>. The locals built up for a response are applied to those given to the view rendering methods such as <code>res.render()</code>.</p>1432-1433-<pre><code> app.all('/movie/:id', function(req, res, next){1434- Movie.get(req.params.id, function(err, movie){1435- // Assigns res.locals.movie = movie1436- res.local('movie', movie);1437- });1438- });1439-1440- app.get('/movie/:id', function(req, res){1441- // movie is already a local, however we1442- // can pass more if we wish1443- res.render('movie', { displayReviews: true });1444- });1445-</code></pre>1446-1447-<h3 id="res.locals()">res.locals(obj)</h3>1448-1449-<p> Assign several locals with the given <em>obj</em>. The following are equivalent:</p>1450-1451-<pre><code> res.local('foo', bar);1452- res.local('bar', baz);1453-1454- res.locals({ foo: bar, bar: baz });1455-</code></pre>1456-1457-<h3 id="app.set()">app.set(name[, val])</h3>1458-1459-<p>Apply an application level setting <em>name</em> to <em>val</em>, or1460-get the value of <em>name</em> when <em>val</em> is not present:</p>1461-1462-<pre><code>app.set('views', __dirname + '/views');1463-app.set('views');1464-// => ...path...1465-</code></pre>1466-1467-<p>Alternatively you may simply access the settings via <em>app.settings</em>:</p>1468-1469-<pre><code>app.settings.views1470-// => ...path...1471-</code></pre>1472-1473-<h3 id="app.enable()">app.enable(name)</h3>1474-1475-<p>Enable the given setting <em>name</em>:</p>1476-1477-<pre><code>app.enable('some arbitrary setting');1478-app.set('some arbitrary setting');1479-// => true1480-1481-app.enabled('some arbitrary setting');1482-// => true1483-</code></pre>1484-1485-<h3 id="app.enabled()">app.enabled(name)</h3>1486-1487-<p>Check if setting <em>name</em> is enabled:</p>1488-1489-<pre><code>app.enabled('view cache');1490-// => false1491-1492-app.enable('view cache');1493-app.enabled('view cache');1494-// => true1495-</code></pre>1496-1497-<h3 id="app.disable()">app.disable(name)</h3>1498-1499-<p>Disable the given setting <em>name</em>:</p>1500-1501-<pre><code>app.disable('some setting');1502-app.set('some setting');1503-// => false1504-1505-app.disabled('some setting');1506-// => false1507-</code></pre>1508-1509-<h3 id="app.disabled()">app.disabled(name)</h3>1510-1511-<p>Check if setting <em>name</em> is disabled:</p>1512-1513-<pre><code>app.enable('view cache');1514-1515-app.disabled('view cache');1516-// => false1517-1518-app.disable('view cache');1519-app.disabled('view cache');1520-// => true1521-</code></pre>1522-1523-<h3 id="app.configure()">app.configure(env|function[, function])</h3>1524-1525-<p>Define a callback function for the given <em>env</em> (or all environments) with callback <em>function</em>:</p>1526-1527-<pre><code>app.configure(function(){1528- // executed for each env1529-});1530-1531-app.configure('development', function(){1532- // executed for 'development' only1533-});1534-</code></pre>1535-1536-<h3 id="app.redirect()">app.redirect(name, val)</h3>1537-1538-<p>For use with <em>res.redirect()</em> we can map redirects at the application level as shown below:</p>1539-1540-<pre><code>app.redirect('google', 'http://google.com');1541-</code></pre>1542-1543-<p>Now in a route we may call:</p>1544-1545-<p> res.redirect(‘google’);</p>1546-1547-<p>We may also map dynamic redirects:</p>1548-1549-<pre><code>app.redirect('comments', function(req, res){1550- return '/post/' + req.params.id + '/comments';1551-});1552-</code></pre>1553-1554-<p>So now we may do the following, and the redirect will dynamically adjust to1555-the context of the request. If we called this route with <em>GET /post/12</em> our1556-redirect <em>Location</em> would be <em>/post/12/comments</em>.</p>1557-1558-<pre><code>app.get('/post/:id', function(req, res){1559- res.redirect('comments');1560-});1561-</code></pre>1562-1563-<p>When mounted, <em>res.redirect()</em> will respect the mount-point. For example if a blog app is mounted at <em>/blog</em>, the following will redirect to <em>/blog/posts</em>:</p>1564-1565-<pre><code>res.redirect('/posts');1566-</code></pre>1567-1568-<h3 id="app.error()">app.error(function)</h3>1569-1570-<p>Adds an error handler <em>function</em> which will receive the exception as the first parameter as shown below.1571-Note that we may set several error handlers by making several calls to this method, however the handler1572-should call <em>next(err)</em> if it does not wish to deal with the exception:</p>1573-1574-<pre><code>app.error(function(err, req, res, next){1575- res.send(err.message, 500);1576-});1577-</code></pre>1578-1579-<h3 id="app.helpers()">app.helpers(obj)</h3>1580-1581-<p>Registers static view helpers.</p>1582-1583-<pre><code>app.helpers({1584- name: function(first, last){ return first + ', ' + last }1585- , firstName: 'tj'1586- , lastName: 'holowaychuk'1587-});1588-</code></pre>1589-1590-<p>Our view could now utilize the <em>firstName</em> and <em>lastName</em> variables,1591-as well as the <em>name()</em> function exposed.</p>1592-1593-<pre><code><%= name(firstName, lastName) %>1594-</code></pre>1595-1596-<p>Express also provides a few locals by default:</p>1597-1598-<pre><code>- `settings` the app's settings object1599-- `filename` the view's filename1600-- `layout(path)` specify the layout from within a view1601-</code></pre>1602-1603-<p>This method is aliased as <em>app.locals()</em>.</p>1604-1605-<h3 id="app.dynamichelpers()">app.dynamicHelpers(obj)</h3>1606-1607-<p>Registers dynamic view helpers. Dynamic view helpers1608-are simply functions which accept <em>req</em>, <em>res</em>, and are1609-evaluated against the <em>Server</em> instance before a view is rendered. The <em>return value</em> of this function1610-becomes the local variable it is associated with.</p>1611-1612-<pre><code>app.dynamicHelpers({1613- session: function(req, res){1614- return req.session;1615- }1616-});1617-</code></pre>1618-1619-<p>All views would now have <em>session</em> available so that session data can be accessed via <em>session.name</em> etc:</p>1620-1621-<pre><code><%= session.name %>1622-</code></pre>1623-1624-<h3 id="app.lookup">app.lookup</h3>1625-1626-<p> The <em>app.lookup</em> http methods returns an array of callback functions1627- associated with the given <em>path</em>.</p>1628-1629-<p> Suppose we define the following routes:</p>1630-1631-<pre><code> app.get('/user/:id', function(){});1632- app.put('/user/:id', function(){});1633- app.get('/user/:id/:op?', function(){});1634-</code></pre>1635-1636-<p> We can utilize this lookup functionality to check which routes1637- have been defined, which can be extremely useful for higher level1638- frameworks built on Express.</p>1639-1640-<pre><code> app.lookup.get('/user/:id');1641- // => [Function]1642-1643- app.lookup.get('/user/:id/:op?');1644- // => [Function]1645-1646- app.lookup.put('/user/:id');1647- // => [Function]1648-1649- app.lookup.all('/user/:id');1650- // => [Function, Function]1651-1652- app.lookup.all('/hey');1653- // => []1654-</code></pre>1655-1656-<p> To alias <em>app.lookup.VERB()</em>, we can simply invoke <em>app.VERB()</em>1657- without a callback, as a shortcut, for example the following are1658- equivalent:</p>1659-1660-<pre><code> app.lookup.get('/user');1661- app.get('/user');1662-</code></pre>1663-1664-<p> Each function returned has the following properties:</p>1665-1666-<pre><code> var fn = app.get('/user/:id/:op?')[0];1667-1668- fn.regexp1669- // => /^\/user\/(?:([^\/]+?))(?:\/([^\/]+?))?\/?$/i1670-1671- fn.keys1672- // => ['id', 'op']1673-1674- fn.path1675- // => '/user/:id/:op?'1676-1677- fn.method1678- // => 'GET'1679-</code></pre>1680-1681-<h3 id="app.match">app.match</h3>1682-1683-<p> The <em>app.match</em> http methods return an array of callback functions1684- which match the given <em>url</em>, which may include a query string etc. This1685- is useful when you want reflect on which routes have the opportunity to1686- respond.</p>1687-1688-<p> Suppose we define the following routes:</p>1689-1690-<pre><code> app.get('/user/:id', function(){});1691- app.put('/user/:id', function(){});1692- app.get('/user/:id/:op?', function(){});1693-</code></pre>1694-1695-<p> Our match against <strong>GET</strong> will return two functions, since the <em>:op</em>1696- in our second route is optional.</p>1697-1698-<pre><code> app.match.get('/user/1');1699- // => [Function, Function]1700-</code></pre>1701-1702-<p> This second call returns only the callback for <em>/user/:id/:op?</em>.</p>1703-1704-<pre><code> app.match.get('/user/23/edit');1705- // => [Function]1706-</code></pre>1707-1708-<p> We can also use <em>all()</em> to disregard the http method:</p>1709-1710-<pre><code> app.match.all('/user/20');1711- // => [Function, Function, Function]1712-</code></pre>1713-1714-<p> Each function matched has the following properties:</p>1715-1716-<pre><code> var fn = app.match.get('/user/23/edit')[0];1717-1718- fn.keys1719- // => ['id', 'op']1720-1721- fn.params1722- // => { id: '23', op: 'edit' }1723-1724- fn.method1725- // => 'GET'1726-</code></pre>1727-1728-<h3 id="app.mounted()">app.mounted(fn)</h3>1729-1730-<p>Assign a callback <em>fn</em> which is called when this <em>Server</em> is passed to <em>Server#use()</em>.</p>1731-1732-<pre><code>var app = express.createServer(),1733- blog = express.createServer();1734-1735-blog.mounted(function(parent){1736- // parent is app1737- // "this" is blog1738-});1739-1740-app.use(blog);1741-</code></pre>1742-1743-<h3 id="app.register()">app.register(ext, exports)</h3>1744-1745-<p>Register the given template engine <em>exports</em>1746-as <em>ext</em>. For example we may wish to map “.html”1747-files to jade:</p>1748-1749-<pre><code> app.register('.html', require('jade'));1750-</code></pre>1751-1752-<p>This is also useful for libraries that may not1753-match extensions correctly. For example my haml.js1754-library is installed from npm as “hamljs” so instead1755-of layout.hamljs, we can register the engine as “.haml”:</p>1756-1757-<pre><code> app.register('.haml', require('haml-js'));1758-</code></pre>1759-1760-<p>For engines that do not comply with the Express1761-specification, we can also wrap their api this way. Below1762-we map <em>.md</em> to render markdown files, rendering the html once1763-since it will not change on subsequent calls, and support local substitution1764-in the form of “{name}”.</p>1765-1766-<pre><code> app.register('.md', {1767- compile: function(str, options){1768- var html = md.toHTML(str);1769- return function(locals){1770- return html.replace(/\{([^}]+)\}/g, function(_, name){1771- return locals[name];1772- });1773- };1774- }1775- });1776-</code></pre>1777-1778-<h3 id="app.listen()">app.listen([port[, host]])</h3>1779-1780-<p>Bind the app server to the given <em>port</em>, which defaults to 3000. When <em>host</em> is omitted all1781-connections will be accepted via <em>INADDR_ANY</em>.</p>1782-1783-<pre><code>app.listen();1784-app.listen(3000);1785-app.listen(3000, 'n.n.n.n');1786-</code></pre>1787-1788-<p>The <em>port</em> argument may also be a string representing the path to a unix domain socket:</p>1789-1790-<pre><code>app.listen('/tmp/express.sock');1791-</code></pre>1792-1793-<p>Then try it out:</p>1794-1795-<pre><code>$ telnet /tmp/express.sock1796-GET / HTTP/1.11797-1798-HTTP/1.1 200 OK1799-Content-Type: text/plain1800-Content-Length: 111801-1802-Hello World1803-</code></pre>1804- </div>1805- </div>1806- </body>1807-</html>deleted:docs/guide.md-1366 removed
1-### Installation2-3- $ npm install express4-5-or to access the `express(1)` executable install globally:6-7- $ npm install -g express8-9-## Quick Start10-11- The quickest way to get started with express is to utilize the executable `express(1)` to generate an application as shown below:12-13- Create the app:14-15- $ npm install -g express16- $ express /tmp/foo && cd /tmp/foo17-18- Install dependencies:19-20- $ npm install -d21-22- Start the server:23-24- $ node app.js25-26-### Creating A Server27-28- 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()_.29-30- var app = require('express').createServer();31- 32- app.get('/', function(req, res){33- res.send('hello world');34- });35- 36- app.listen(3000);37-38-### Creating An HTTPS Server39-40- To initialize a _express.HTTPSServer_ we do the same as above, however we41- 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).42- 43- var app = require('express').createServer({ key: ... });44-45-### Configuration46-47-Express supports arbitrary environments, such as _production_ and _development_. Developers48-can use the _configure()_ method to setup needs required by the current environment. When49-_configure()_ is called without an environment name it will be run in _every_ environment 50-prior to the environment specific callback.51-52-In the example below we only _dumpExceptions_, and respond with exception stack traces53-in _development_ mode, however for both environments we utilize _methodOverride_ and _bodyParser_.54-Note the use of _app.router_, which can (optionally) be used to mount the application routes,55-otherwise the first call to _app.get()_, _app.post()_, etc will mount the routes.56-57- app.configure(function(){58- app.use(express.methodOverride());59- app.use(express.bodyParser());60- app.use(app.router);61- });62- 63- app.configure('development', function(){64- app.use(express.static(__dirname + '/public'));65- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));66- });67- 68- app.configure('production', function(){69- var oneYear = 31557600000;70- app.use(express.static(__dirname + '/public', { maxAge: oneYear }));71- app.use(express.errorHandler());72- });73-74-For similar environments you may also pass several env strings:75-76- app.configure('stage', 'prod', function(){77- // config78- });79-80-For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:81-82- app.configure(function(){83- app.set('views', __dirname + '/views');84- app.set('views');85- // => "/absolute/path/to/views"86- 87- app.enable('some feature');88- // same as app.set('some feature', true);89- 90- app.disable('some feature');91- // same as app.set('some feature', false);92- 93- app.enabled('some feature')94- // => false95- });96-97-To alter the environment we can set the _NODE_ENV_ environment variable, for example:98-99- $ NODE_ENV=production node app.js100-101-This is _very_ important, as many caching mechanisms are _only enabled_ when in production.102-103-### Settings104-105-Express supports the following settings out of the box:106-107- * _home_ Application base path used for _res.redirect()_ and transparently handling mounted apps.108- * _views_ Root views directory defaulting to **CWD/views**109- * _view engine_ Default view engine name for views rendered without extensions110- * _view cache_ Enable view caching (enabled in production)111- * _charset_ Alter the default charset of "utf-8"112- * _case sensitive routes_ Enable case-sensitive routing113- * _strict routing_ When enabled trailing slashes are no longer ignored114- * _jsonp callback_ Enable _res.send()_ / _res.json()_ transparent jsonp support115-116-### Routing117-118-Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.119-For example we may want to render a user's account for the path _/user/12_, this120-can be done by defining the route below. The values associated to the named placeholders 121-are available as `req.params`.122-123- app.get('/user/:id', function(req, res){124- res.send('user ' + req.params.id);125- });126-127-A route is simple a string which is compiled to a _RegExp_ internally. For example128-when _/user/:id_ is compiled, a simplified version of the regexp may look similar to:129-130- \/user\/([^\/]+)\/?131-132-Regular expression literals may also be passed for complex uses. Since capture133-groups with literal _RegExp_'s are anonymous we can access them directly `req.params`. So our first capture group would be _req.params[0]_ and the second would follow as _req.params[1]_.134-135- app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, function(req, res){136- res.send(req.params);137- });138-139-Curl requests against the previously defined route:140-141- $ curl http://dev:3000/user142- [null,null]143- $ curl http://dev:3000/users144- [null,null]145- $ curl http://dev:3000/users/1146- ["1",null]147- $ curl http://dev:3000/users/1..15148- ["1","15"]149-150-Below are some route examples, and the associated paths that they151-may consume:152-153- "/user/:id"154- /user/12155-156- "/users/:id?"157- /users/5158- /users159-160- "/files/*"161- /files/jquery.js162- /files/javascripts/jquery.js163-164- "/file/*.*"165- /files/jquery.js166- /files/javascripts/jquery.js167- 168- "/user/:id/:operation?"169- /user/1170- /user/1/edit171- 172- "/products.:format"173- /products.json174- /products.xml175-176- "/products.:format?"177- /products.json178- /products.xml179- /products180- 181- "/user/:id.:format?"182- /user/12183- /user/12.json184-185-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_:186-187- var express = require('express')188- , app = express.createServer();189-190- app.use(express.bodyParser());191-192- app.post('/', function(req, res){193- res.send(req.body);194- });195-196- app.listen(3000);197-198-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([0-9]+)'_ which will _not_ match unless the placeholder value contains only digits.199-200-### Passing Route Control201-202-We may pass control to the next _matching_ route, by calling the _third_ argument,203-the _next()_ function. When a match cannot be made, control is passed back to Connect,204-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.205-206- app.get('/users/:id?', function(req, res, next){207- var id = req.params.id;208- if (id) {209- // do something210- } else {211- next();212- }213- });214- 215- app.get('/users', function(req, res){216- // do something else217- });218-219-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_.220-221- var express = require('express')222- , app = express.createServer();223-224- var users = [{ name: 'tj' }];225-226- app.all('/user/:id/:op?', function(req, res, next){227- req.user = users[req.params.id];228- if (req.user) {229- next();230- } else {231- next(new Error('cannot find user ' + req.params.id));232- }233- });234-235- app.get('/user/:id', function(req, res){236- res.send('viewing ' + req.user.name);237- });238-239- app.get('/user/:id/edit', function(req, res){240- res.send('editing ' + req.user.name);241- });242-243- app.put('/user/:id', function(req, res){244- res.send('updating ' + req.user.name);245- });246-247- app.get('*', function(req, res){248- res.send(404, 'what???');249- });250-251- app.listen(3000); 252-253-### Middleware254-255-Middleware via [Connect](http://github.com/senchalabs/connect) can be256-passed to _express.createServer()_ as you would with a regular Connect server. For example:257-258- var express = require('express');259-260- var app = express.createServer(261- express.logger()262- , express.bodyParser()263- );264-265-Alternatively we can _use()_ them which is useful when adding middleware within _configure()_ blocks, in a progressive manner.266-267- app.use(express.logger({ format: ':method :url' }));268-269-Typically with connect middleware you would _require('connect')_ like so:270-271- var connect = require('connect');272- app.use(connect.logger());273- app.use(connect.bodyParser());274-275-This is somewhat annoying, so express re-exports these middleware properties, however they are _identical_:276-277- app.use(express.logger());278- app.use(express.bodyParser());279-280-Middleware ordering is important, when Connect receives a request the _first_ middleware we pass to _createServer()_ or _use()_ is executed with three parameters, _request_, _response_, and a callback function usually named _next_. When _next()_ is invoked the second middleware will then have it's turn and so on. This is important to note because many middleware depend on each other, for example _methodOverride()_ checks _req.body._method_ for the HTTP method override, however _bodyParser()_ parses the request body and populates _req.body_. Another example of this is cookie parsing and session support, we must first _use()_ _cookieParser()_ followed by _session()_.281-282-Many Express applications may contain the line _app.use(app.router)_, while this may appear strange, it'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 _last_ middleware so that any exception passed to _next()_ 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 below283-284- app.use(express.logger(...));285- app.use(express.bodyParser(...));286- app.use(express.cookieParser(...));287- app.use(express.session(...));288- app.use(app.router);289- app.use(express.static(...));290- app.use(express.errorHandler(...));291-292-First we add _logger()_ so that it may wrap node's _req.end()_ method, providing us with response-time data. Next the request's body will be parsed (if any), followed by cookie parsing and session support, meaning _req.session_ will be defined by the time we hit our routes in _app.router_. If a request such as _GET /javascripts/jquery.js_ is handled by our routes, and we do not call _next()_ then the _static()_ 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.293-294- var downloads = {};295-296- app.use(app.router);297- app.use(express.static(__dirname + '/public'));298-299- app.get('/*', function(req, res, next){300- var file = req.params[0];301- downloads[file] = downloads[file] || 0;302- downloads[file]++;303- next();304- });305-306-307-### Route Middleware308-309-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.310-311-Typically async data retrieval might look similar to below, where we take the _:id_ parameter, and attempt loading a user. 312-313- app.get('/user/:id', function(req, res, next){314- loadUser(req.params.id, function(err, user){315- if (err) return next(err);316- res.send('Viewing user ' + user.name);317- });318- });319-320-To keep things DRY and to increase readability we can apply this logic within a middleware. As you can see below, abstracting this logic into middleware allows us to reuse it, and clean up our route at the same time. 321-322- function loadUser(req, res, next) {323- // You would fetch your user from the db324- var user = users[req.params.id];325- if (user) {326- req.user = user;327- next();328- } else {329- next(new Error('Failed to load user ' + req.params.id));330- }331- }332- 333- app.get('/user/:id', loadUser, function(req, res){334- res.send('Viewing user ' + req.user.name);335- });336-337-Multiple route middleware can be applied, and will be executed sequentially to apply further logic such as restricting access to a user account. In the example below only the authenticated user may edit his/her account.338-339- function andRestrictToSelf(req, res, next) {340- req.authenticatedUser.id == req.user.id341- ? next()342- : next(new Error('Unauthorized'));343- }344- 345- app.get('/user/:id/edit', loadUser, andRestrictToSelf, function(req, res){346- res.send('Editing user ' + req.user.name);347- });348-349-Keeping in mind that middleware are simply functions, we can define function that _returns_ the middleware in order to create a more expressive and flexible solution as shown below.350-351- function andRestrictTo(role) {352- return function(req, res, next) {353- req.authenticatedUser.role == role354- ? next()355- : next(new Error('Unauthorized'));356- }357- }358- 359- app.del('/user/:id', loadUser, andRestrictTo('admin'), function(req, res){360- res.send('Deleted user ' + req.user.name);361- });362-363-Commonly used "stacks" of middleware can be passed as an array (_applied recursively_), which can be mixed and matched to any degree.364-365- var a = [middleware1, middleware2]366- , b = [middleware3, middleware4]367- , all = [a, b];368- 369- app.get('/foo', a, function(){});370- app.get('/bar', a, function(){});371- 372- app.get('/', a, middleware3, middleware4, function(){});373- app.get('/', a, b, function(){});374- app.get('/', all, function(){});375-376-For this example in full, view the [route middleware example](http://github.com/visionmedia/express/blob/master/examples/route-middleware/app.js) in the repository.377-378-There are times when we may want to "skip" passed remaining route middleware, but continue matching subsequent routes. To do this we invoke `next()` with the string "route" `next('route')`. If no remaining routes match the request url then Express will respond with 404 Not Found.379-380-### HTTP Methods381-382-We have seen _app.get()_ a few times, however Express also exposes other familiar HTTP verbs in the same manner, such as _app.post()_, _app.del()_, etc.383-384- A common example for _POST_ usage, is when "submitting" a form. Below we simply set our form method to "post" in our html, and control will be given to the route we have defined below it.385- 386- <form method="post" action="/">387- <input type="text" name="user[name]" />388- <input type="text" name="user[email]" />389- <input type="submit" value="Submit" />390- </form>391-392-By default Express does not know what to do with this request body, so we should add the _bodyParser_ middleware, which will parse _application/x-www-form-urlencoded_ and _application/json_ request bodies and place the variables in _req.body_. We can do this by "using" the middleware as shown below:393-394- app.use(express.bodyParser());395-396-Our route below will now have access to the _req.body.user_ object which will contain the _name_ and _email_ properties when defined.397-398- app.post('/', function(req, res){399- console.log(req.body.user);400- res.redirect('back');401- });402-403-When using methods such as _PUT_ with a form, we can utilize a hidden input named _\_method_, which can be used to alter the HTTP method. To do so we first need the _methodOverride_ middleware, which should be placed below _bodyParser_ so that it can utilize it's _req.body_ containing the form values.404-405- app.use(express.bodyParser());406- app.use(express.methodOverride());407-408-The reason that these are not always defaults, is simply because these are not required for Express to be fully functional. Depending on the needs of your application, you may not need these at all, your methods such as _PUT_ and _DELETE_ can still be accessed by clients which can use them directly, although _methodOverride_ provides a great solution for forms. Below shows what the usage of _PUT_ might look like:409-410- <form method="post" action="/">411- <input type="hidden" name="_method" value="put" />412- <input type="text" name="user[name]" />413- <input type="text" name="user[email]" />414- <input type="submit" value="Submit" /> 415- </form>416-417- app.put('/', function(){418- console.log(req.body.user);419- res.redirect('back');420- });421-422-### Error Handling423-424- Error handling middleware are simply middleware with an arity of 4, aka425- the signature _(err, req, res, next)_. When you _next(err)_ an error,426- only these middleware are executed and have a chance to respond. For example:427- 428- app.use(app.bodyParser());429- app.use(app.methodOverride());430- app.use(app.router);431- app.use(function(err, req, res, next){432- res.send(500, 'Server error');433- });434-435-### Route Param Pre-conditions436-437-Route param pre-conditions can drastically improve the readability of your application, through implicit loading of data, and validation of request urls. For example if you are constantly fetching common data for several routes, such as loading a user for _/user/:id_, we might typically do something like below:438-439- app.get('/user/:userId', function(req, res, next){440- User.get(req.params.userId, function(err, user){441- if (err) return next(err);442- res.send('user ' + user.name);443- });444- }); 445-446-With preconditions our params can be mapped to callbacks which may perform validation, coercion, or even loading data from a database. Below we invoke _app.param()_ with the parameter name we wish to map to some middleware, as you can see we receive the _id_ argument which contains the placeholder value. Using this we load the user and perform error handling as usual, and simple call _next()_ to pass control to the next precondition or route handler.447-448- app.param('userId', function(req, res, next, id){449- User.get(id, function(err, user){450- if (err) return next(err);451- if (!user) return next(new Error('failed to find user'));452- req.user = user;453- next();454- });455- });456-457-Doing so, as mentioned drastically improves our route readability, and allows us to easily share this logic throughout our application:458-459- app.get('/user/:userId', function(req, res){460- res.send('user ' + req.user.name);461- });462-463-### View Rendering464-465-View filenames take the form "<name>.<engine>", where <engine> is the name466-of the module that will be required. For example the view _layout.ejs_ will467-tell the view system to _require('ejs')_, the module being loaded must export the method _exports.compile(str, options)_, and return a _Function_ to comply with Express. To alter this behaviour468-_app.register()_ can be used to map engines to file extensions, so that for example "foo.html" can be rendered by ejs.469-470-Below is an example using [Jade](http://github.com/visionmedia/jade) to render _index.html_,471-and since we do not use _layout: false_ the rendered contents of _index.jade_ will be passed as 472-the _body_ local variable in _layout.jade_.473-474- app.get('/', function(req, res){475- res.render('index.jade', { title: 'My Site' });476- });477-478-The new _view engine_ setting allows us to specify our default template engine,479-so for example when using jade we could set:480-481- app.set('view engine', 'jade');482-483-Allowing us to render with:484-485- res.render('index');486-487-vs:488-489- res.render('index.jade');490-491-When _view engine_ is set, extensions are entirely optional, however we can still492-mix and match template engines:493-494- res.render('another-page.ejs');495-496- To apply application-level locals, or view engine options may be set using _app.local()_ or _app.locals()_, for example if we don't want layouts for most of our templates we may do:497-498- app.local('layout', false);499-500-Which can then be overridden within the _res.render()_ call if desired, and is otherwise functionally equivalent to passing directly to `res.render()`:501-502- res.render('myview.ejs', { layout: true });503-504-When an alternate layout is required, we may also specify a path. For example if we have _view engine_ set to _jade_ and a file named _./views/mylayout.jade_ we can simply pass:505-506- res.render('page', { layout: 'mylayout' });507-508-Otherwise we must specify the extension:509-510- res.render('page', { layout: 'mylayout.jade' });511-512-These paths may also be absolute:513-514- res.render('page', { layout: __dirname + '/../../mylayout.jade' });515-516-### View Partials517-518-The Express view system has built-in support for partials and collections, which are "mini" views representing a document fragment. For example rather than iterating519-in a view to display comments, we could use partial collection:520-521- partial('comment', { collection: comments });522-523-If no other options or local variables are desired, we can omit the object and simply pass our array, which is equivalent to above:524-525- partial('comment', comments);526-527-When using the partial collection support a few "magic" locals are provided528-for free:529-530- * _firstInCollection_ true if this is the first object531- * _indexInCollection_ index of the object in the collection532- * _lastInCollection_ true if this is the last object533- * _collectionLength_ length of the collection534-535-Local variables passed (or generated) take precedence, however locals passed to the parent view are available in the child view as well. So for example if we were to render a blog post with _partial('blog/post', post)_ it would generate the _post_ local, but the view calling this function had the local _user_, it would be available to the _blog/post_ view as well.536-537-For documentation on altering the object name view [res.partial()](http://expressjs.com/guide.html#res-partial-view-options-).538-539-__NOTE:__ be careful about when you use partial collections, as rendering an array with a length of 100 means we have to render 100 views. For simple collections you may inline the iteration instead of using partial collection support to decrease overhead.540-541-### View Lookup542-543-View lookup is performed relative to the parent view, for example if we had a page view named _views/user/list.jade_, and within that view we did _partial('edit')_ it would attempt to load _views/user/edit.jade_, whereas _partial('../messages')_ would load _views/messages.jade_.544-545-The view system also allows for index templates, allowing you to have a directory of the same name. For example within a route we may have _res.render('users')_ either _views/users.jade_, or _views/users/index.jade_.546-547-When utilizing index views as shown above, we may reference _views/users/index.jade_ from a view in the same directory by _partial('users')_, and the view system will try _../users/index_, preventing us from needing to call _partial('index')_.548-549-### Template Engines550-551-Below are a few template engines commonly used with Express:552-553- * [Haml](http://github.com/visionmedia/haml.js) haml implementation554- * [Jade](http://jade-lang.com) haml.js successor555- * [EJS](http://github.com/visionmedia/ejs) Embedded JavaScript556- * [CoffeeKup](http://github.com/mauricemach/coffeekup) CoffeeScript based templating557- * [jQuery Templates](https://github.com/kof/node-jqtpl) for node558-559-### Session Support560-561-Sessions support can be added by using Connect's _session_ middleware. To do so we also need the _cookieParser_ middleware place above it, which will parse and populate cookie data to _req.cookies_.562-563- app.use(express.cookieParser());564- app.use(express.session({ secret: "keyboard cat" }));565-566-By default the _session_ middleware uses the memory store bundled with Connect, however many implementations exist. For example [connect-redis](http://github.com/visionmedia/connect-redis) supplies a [Redis](http://code.google.com/p/redis/) session store and can be used as shown below:567-568- var RedisStore = require('connect-redis')(express);569- app.use(express.cookieParser());570- app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));571-572-Now the _req.session_ and _req.sessionStore_ properties will be accessible to all routes and subsequent middleware. Properties on _req.session_ are automatically saved on a response, so for example if we wish to shopping cart data:573-574- var RedisStore = require('connect-redis')(express);575- app.use(express.bodyParser());576- app.use(express.cookieParser());577- app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));578-579- app.post('/add-to-cart', function(req, res){580- // Perhaps we posted several items with a form581- // (use the bodyParser() middleware for this)582- var items = req.body.items;583- req.session.items = items;584- res.redirect('back');585- });586-587- app.get('/add-to-cart', function(req, res){588- // When redirected back to GET /add-to-cart589- // we could check req.session.items && req.session.items.length590- // to print out a message591- if (req.session.items && req.session.items.length) {592- req.notify('info', 'You have %s items in your cart', req.session.items.length);593- }594- res.render('shopping-cart');595- });596-597-The _req.session_ object also has methods such as _Session#touch()_, _Session#destroy()_, _Session#regenerate()_ among others to maintain and manipulate sessions. For more information view the [Connect Session](http://senchalabs.github.com/connect/middleware-session.html) documentation.598-599-### Migration Guide600-601- Express 1.x developers may reference the [Migration Guide](migrate.html) to get up to speed on how to upgrade your application to work with Express 2.x, Connect 1.x, and Node 0.4.x.602-603-### req.header(key[, defaultValue])604-605-Get the case-insensitive request header _key_, with optional _defaultValue_:606-607- req.header('Host');608- req.header('host');609- req.header('Accept', '*/*');610-611-The _Referrer_ and _Referer_ header fields are special-cased, either will work:612-613- // sent Referrer: http://google.com614-615- req.header('Referer');616- // => "http://google.com"617-618- req.header('Referrer');619- // => "http://google.com"620-621-### req.accepts(type)622-623-Check if the _Accept_ header is present, and includes the given _type_.624-625-When the _Accept_ header is not present _true_ is returned. Otherwise626-the given _type_ is matched by an exact match, and then subtypes. You627-may pass the subtype such as "html" which is then converted internally628-to "text/html" using the mime lookup table.629-630- // Accept: text/html631- req.accepts('html');632- // => true633-634- // Accept: text/*; application/json635- req.accepts('html');636- req.accepts('text/html');637- req.accepts('text/plain');638- req.accepts('application/json');639- // => true640-641- req.accepts('image/png');642- req.accepts('png');643- // => false644-645-### req.is(type)646-647-Check if the incoming request contains the _Content-Type_648-header field, and it contains the give mime _type_.649- 650- // With Content-Type: text/html; charset=utf-8651- req.is('html');652- req.is('text/html');653- // => true654- 655- // When Content-Type is application/json656- req.is('json');657- req.is('application/json');658- // => true659- 660- req.is('html');661- // => false662- 663-Ad-hoc callbacks can also be registered with Express, to perform664-assertions again the request, for example if we need an expressive665-way to check if our incoming request is an image, we can register _"an image"_666-callback:667- 668- app.is('an image', function(req){669- return 0 == req.headers['content-type'].indexOf('image');670- });671- 672-Now within our route callbacks, we can use to to assert content types673-such as _"image/jpeg"_, _"image/png"_, etc.674- 675- app.post('/image/upload', function(req, res, next){676- if (req.is('an image')) {677- // do something678- } else {679- next();680- }681- });682-683-Keep in mind this method is _not_ limited to checking _Content-Type_, you684-can perform any request assertion you wish.685-686-Wildcard matches can also be made, simplifying our example above for _"an image"_, by asserting the _subtype_ only:687-688- req.is('image/*');689-690-We may also assert the _type_ as shown below, which would return true for _"application/json"_, and _"text/json"_.691-692- req.is('*/json');693-694-### req.param(name[, default])695-696-Return the value of param _name_ when present or _default_.697-698- - Checks route params (_req.params_), ex: /user/:id699- - Checks query string params (_req.query_), ex: ?id=12700- - Checks urlencoded body params (_req.body_), ex: id=12701-702-To utilize urlencoded request bodies, _req.body_703-should be an object. This can be done by using704-the _express.bodyParser middleware.705-706-### req.get(field, param)707-708- Get _field_'s _param_ value, defaulting to '' when the _param_709- or _field_ is not present.710-711- req.get('content-disposition', 'filename');712- // => "something.png"713-714- req.get('Content-Type', 'boundary');715- // => "--foo-bar-baz"716-717-### req.notify(type[, msg])718-719-Queue flash _msg_ of the given _type_.720-721- req.notify('info', 'email sent');722- req.notify('error', 'email delivery failed');723- req.notify('info', 'email re-sent');724- // => 2725-726- req.notify('info');727- // => ['email sent', 'email re-sent']728-729- req.notify('info');730- // => []731-732- req.notify();733- // => { error: ['email delivery failed'], info: [] }734-735-Flash notification message may also utilize formatters, by default only the %s string and %d integer formatters is available:736-737- req.notify('info', 'email delivery to <em>%s</em> from <em>%s</em> failed.', toUser, fromUser);738-739-Argument HTML is escaped, to prevent XSS, however HTML notification format is valid.740-741-### req.isXMLHttpRequest742-743-Also aliased as _req.xhr_, this getter checks the _X-Requested-With_ header744-to see if it was issued by an _XMLHttpRequest_:745-746- req.xhr747- req.isXMLHttpRequest748-749-### res.header(key[, val])750-751-Get or set the response header _key_.752-753- res.header('Content-Length');754- // => undefined755-756- res.header('Content-Length', 123);757- // => 123758- 759- res.header('Content-Length');760- // => 123761-762-### res.charset763-764-Sets the charset for subsequent `Content-Type` header fields. For example `res.send()` and `res.render()` default to "utf8", so we may explicitly set the charset before rendering a template:765-766- res.charset = 'ISO-8859-1';767- res.render('users');768-769-or before responding with `res.send()`:770-771- res.charset = 'ISO-8859-1';772- res.send(str);773-774-or with node's `res.end()`:775-776- res.charset = 'ISO-8859-1';777- res.header('Content-Type', 'text/plain');778- res.end(str);779-780-### res.contentType(type)781-782-Sets the _Content-Type_ response header to the given _type_.783-784- var filename = 'path/to/image.png';785- res.contentType(filename);786- // Content-Type is now "image/png"787-788-A literal _Content-Type_ works as well:789-790- res.contentType('application/json');791-792-Or simply the extension without leading `.`:793-794- res.contentType('json');795-796-### res.attachment([filename])797-798-Sets the _Content-Disposition_ response header to "attachment", with optional _filename_.799-800- res.attachment('path/to/my/image.png');801-802-### res.status(code)803-804- Sets the `res.statusCode` property to `code` and returns for chaining:805-806- res.status(500).send('Something bad happened');807-808- is equivalent to:809- 810- res.statusCode = 500;811- res.send('Something bad happened');812-813- and:814-815- res.send(500, 'Something bad happened');816-817-### res.sendfile(path[, options[, callback]])818-819-Used by `res.download()` to transfer an arbitrary file. 820-821- res.sendfile('path/to/my.file');822-823-This method accepts an optional callback which is called when824-an error occurs, or when the transfer is complete. By default failures call `next(err)`, however when a callback is supplied you must do this explicitly, or act on the error.825-826- res.sendfile(path, function(err){827- if (err) {828- next(err);829- } else {830- console.log('transferred %s', path);831- }832- });833-834-Options may also be passed to the internal _fs.createReadStream()_ call, for example altering the _bufferSize_:835-836- res.sendfile(path, { bufferSize: 1024 }, function(err){837- // handle838- });839-840-### res.download(file[, filename[, callback[, callback2]]])841-842-Transfer the given _file_ as an attachment with optional alternative _filename_.843-844- res.download('path/to/image.png');845- res.download('path/to/image.png', 'foo.png');846-847-This is equivalent to:848-849- res.attachment(file);850- res.sendfile(file);851-852-An optional callback may be supplied as either the second or third argument, which is passed to _res.sendfile()_. Within this callback you may still respond, as the header has not been sent.853-854- res.download(path, 'expenses.doc', function(err){855- // handle856- });857-858-An optional second callback, _callback2_ may be given to allow you to act on connection related errors, however you should not attempt to respond.859-860- res.download(path, function(err){861- // error or finished862- }, function(err){863- // connection related error864- });865-866-### res.send(body|status[, body])867-868-The _res.send()_ method is a high level response utility allowing you to pass869-objects to respond with json, strings for html, Buffer instances, or numbers representing the status code. The following are all valid uses:870-871- res.send(new Buffer('wahoo'));872- res.send({ some: 'json' });873- res.send(201, { message: 'User created' });874- res.send('<p>some html</p>');875- res.send(404, 'Sorry, cant find that');876- res.send(404); // "Not Found"877- res.send(500); // "Internal Server Error"878-879-The _Content-Type_ response header is defaulted appropriately unless previously defined via `res.header()` / `res.contentType()` etc.880-881-Note that this method _end()_s the response, so you will want to use node's _res.write()_ for multiple writes or streaming.882-883-### res.json(obj|status[, obj])884-885- Send an explicit JSON response. This method is ideal for JSON-only APIs, while it is much like _res.send(obj)_, send is not ideal for cases when you want to send for example a single string as JSON, since the default for _res.send(string)_ is text/html.886-887- res.json(null);888- res.json({ user: 'tj' });889- res.json(500, 'oh noes!');890- res.json(404, 'I dont have that');891-892-### res.redirect(url[, status])893-894-Redirect to the given _url_ with a default response _status_ of 302.895-896- res.redirect('/', 301);897- res.redirect('/account');898- res.redirect('http://google.com');899- res.redirect('home');900- res.redirect('back');901-902-Express supports "redirect mapping", which by default provides _home_, and _back_.903-The _back_ map checks the _Referrer_ and _Referer_ headers, while _home_ utilizes904-the "home" setting and defaults to "/".905-906-### res.cookie(name, val[, options])907-908-Sets the given cookie _name_ to _val_, with options _httpOnly_, _secure_, _expires_ etc. The _path_ option defaults to the app's "home" setting, which909-is typically "/".910-911- // "Remember me" for 15 minutes 912- res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000), httpOnly: true });913-914-The _maxAge_ property may be used to set _expires_ relative to _Date.now()_ in milliseconds, so our example above can now become:915-916- res.cookie('rememberme', 'yes', { maxAge: 900000 });917-918-To parse incoming _Cookie_ headers, use the _cookieParser_ middleware, which provides the _req.cookies_ object:919-920- app.use(express.cookieParser());921- 922- app.get('/', function(req, res){923- // use req.cookies.rememberme924- });925-926-### res.clearCookie(name[, options])927-928-Clear cookie _name_ by setting "expires" far in the past. Much like929-_res.cookie()_ the _path_ option also defaults to the "home" setting.930-931- res.clearCookie('rememberme');932-933-### res.render(view[, options[, fn]])934-935-Render _view_ with the given _options_ and optional callback _fn_.936-When a callback function is given a response will _not_ be made937-automatically, however otherwise a response of _200_ and _text/html_ is given.938-939-The _options_ passed are the local variables as well, for example if we want to expose "user" to the view, and prevent a local we do so within the same object:940-941- var user = { name: 'tj' };942- res.render('index', { layout: false, user: user });943-944-This _options_ object is also considered an "options" object. For example 945-when you pass the _status_ local, it's not only available to the view, it946-sets the response status to this number. This is also useful if a template947-engine accepts specific options, such as _debug_, or _compress_. Below948-is an example of how one might render an error page, passing the _status_ for949-display, as well as it setting _res.statusCode_.950-951- res.render('error', { status: 500, message: 'Internal Server Error' });952-953-### res.partial(view[, options])954-955-Render _view_ partial with the given _options_. This method is always available956-to the view as a local variable.957-958-- _object_ the object named by _as_ or derived from the view name959-- _as_ Variable name for each _collection_ or _object_ value, defaults to the view name.960- * as: 'something' will add the _something_ local variable961- * as: this will use the collection value as the template context962- * as: global will merge the collection value's properties with _locals_963-964-- _collection_ Array of objects, the name is derived from the view name itself. 965- For example _video.html_ will have a object _video_ available to it.966-967-The following are equivalent, and the name of collection value when passed968-to the partial will be _movie_ as derived from the name.969-970- partial('theatre/movie.jade', { collection: movies });971- partial('theatre/movie.jade', movies);972- partial('movie.jade', { collection: movies });973- partial('movie.jade', movies);974- partial('movie', movies);975- // In view: movie.director976-977-To change the local from _movie_ to _video_ we can use the "as" option:978-979- partial('movie', { collection: movies, as: 'video' });980- // In view: video.director981-982-Also we can make our movie the value of _this_ within our view so that instead983-of _movie.director_ we could use _this.director_.984-985- partial('movie', { collection: movies, as: this });986- // In view: this.director987-988-Another alternative is to "expand" the properties of the collection item into989-pseudo globals (local variables) by using _as: global_, which again is syntactic sugar:990-991- partial('movie', { collection: movies, as: global });992- // In view: director993-994-This same logic applies to a single partial object usage:995-996- partial('movie', { object: movie, as: this });997- // In view: this.director998-999- partial('movie', { object: movie, as: global });1000- // In view: director1001-1002- partial('movie', { object: movie, as: 'video' });1003- // In view: video.director1004-1005- partial('movie', { object: movie });1006- // In view: movie.director1007-1008-When a non-collection (does _not_ have _.length_) is passed as the second argument, it is assumed to be the _object_, after which the object's local variable name is derived from the view name:1009-1010- var movie = new Movie('Nightmare Before Christmas', 'Tim Burton')1011- partial('movie', movie)1012- // => In view: movie.director1013-1014-The exception of this, is when a "plain" object, aka "{}" or "new Object" is passed, which is considered an object with local variable. For example some may expect a "movie" local with the following, however since it is a plain object "director" and "title" are simply locals:1015-1016- var movie = { title: 'Nightmare Before Christmas', director: 'Tim Burton' }; 1017- partial('movie', movie)1018-1019-For cases like this where passing a plain object is desired, simply assign it to a key, or use the `object` key which will use the filename-derived variable name. The examples below are equivalent:1020-1021- partial('movie', { locals: { movie: movie }})1022- partial('movie', { movie: movie })1023- partial('movie', { object: movie })1024-1025-This exact API can be utilized from within a route, to respond with a fragment via Ajax or WebSockets, for example we can render a collection of users directly from a route:1026-1027- app.get('/users', function(req, res){1028- if (req.xhr) {1029- // respond with the each user in the collection1030- // passed to the "user" view1031- res.partial('user', users);1032- } else {1033- // respond with layout, and users page1034- // which internally does partial('user', users)1035- // along with other UI1036- res.render('users', { users: users });1037- }1038- });1039-1040-### res.local(name[, val])1041-1042-Get or set the given local variable _name_. The locals built up for a response are applied to those given to the view rendering methods such as `res.render()`.1043-1044- app.all('/movie/:id', function(req, res, next){1045- Movie.get(req.params.id, function(err, movie){1046- // Assigns res.locals.movie = movie1047- res.local('movie', movie);1048- });1049- });1050- 1051- app.get('/movie/:id', function(req, res){1052- // movie is already a local, however we1053- // can pass more if we wish1054- res.render('movie', { displayReviews: true });1055- });1056-1057-### res.locals(obj)1058-1059- Assign several locals with the given _obj_. The following are equivalent:1060- 1061- res.local('foo', bar);1062- res.local('bar', baz);1063-1064- res.locals({ foo: bar, bar, baz });1065-1066-### app.set(name[, val])1067-1068-Apply an application level setting _name_ to _val_, or1069-get the value of _name_ when _val_ is not present:1070-1071- app.set('views', __dirname + '/views');1072- app.set('views');1073- // => ...path...1074-1075-Alternatively you may simply access the settings via _app.settings_:1076-1077- app.settings.views1078- // => ...path...1079-1080-### app.enable(name)1081-1082-Enable the given setting _name_:1083-1084- app.enable('some arbitrary setting');1085- app.set('some arbitrary setting');1086- // => true1087-1088- app.enabled('some arbitrary setting');1089- // => true1090-1091-### app.enabled(name)1092-1093-Check if setting _name_ is enabled:1094-1095- app.enabled('view cache');1096- // => false1097-1098- app.enable('view cache');1099- app.enabled('view cache');1100- // => true1101-1102-### app.disable(name)1103-1104-Disable the given setting _name_:1105-1106- app.disable('some setting');1107- app.set('some setting');1108- // => false1109- 1110- app.disabled('some setting');1111- // => false1112-1113-### app.disabled(name)1114-1115-Check if setting _name_ is disabled:1116-1117- app.enable('view cache');1118-1119- app.disabled('view cache');1120- // => false1121-1122- app.disable('view cache');1123- app.disabled('view cache');1124- // => true1125-1126-### app.configure(env|function[, function])1127-1128-Define a callback function for the given _env_ (or all environments) with callback _function_:1129-1130- app.configure(function(){1131- // executed for each env1132- });1133-1134- app.configure('development', function(){1135- // executed for 'development' only1136- });1137-1138-### app.redirect(name, val)1139-1140-For use with _res.redirect()_ we can map redirects at the application level as shown below:1141-1142- app.redirect('google', 'http://google.com');1143-1144-Now in a route we may call:1145-1146- res.redirect('google');1147-1148-We may also map dynamic redirects:1149-1150- app.redirect('comments', function(req, res){1151- return '/post/' + req.params.id + '/comments';1152- });1153-1154-So now we may do the following, and the redirect will dynamically adjust to1155-the context of the request. If we called this route with _GET /post/12_ our1156-redirect _Location_ would be _/post/12/comments_.1157-1158- app.get('/post/:id', function(req, res){1159- res.redirect('comments');1160- });1161-1162-When mounted, _res.redirect()_ will respect the mount-point. For example if a blog app is mounted at _/blog_, the following will redirect to _/blog/posts_:1163-1164- res.redirect('/posts');1165-1166-### app.locals(obj)1167-1168-Registers static view locals.1169-1170- app.locals({1171- name: function(first, last){ return first + ', ' + last }1172- , firstName: 'tj'1173- , lastName: 'holowaychuk'1174- });1175-1176-Our view could now utilize the _firstName_ and _lastName_ variables,1177-as well as the _name()_ function exposed.1178-1179- <%= name(firstName, lastName) %>1180-1181-Express also provides a few locals by default:1182-1183- - `settings` the app's settings object1184- - `layout(path)` specify the layout from within a view1185-1186-### app.dynamicLocals(obj)1187-1188-Registers dynamic view locals. Dynamic locals1189-are simply functions which accept _req_, _res_, and are1190-evaluated against the _Server_ instance before a view is rendered, and are unique to that specific request. The _return value_ of this function1191-becomes the local variable it is associated with.1192-1193- app.dynamicLocals({1194- session: function(req, res){1195- return req.session;1196- }1197- });1198-1199-All views would now have _session_ available so that session data can be accessed via _session.name_ etc:1200-1201- <%= session.name %>1202-1203-### app.lookup1204-1205- The _app.lookup_ http methods returns an array of callback functions1206- associated with the given _path_.1207-1208- Suppose we define the following routes:1209- 1210- app.get('/user/:id', function(){});1211- app.put('/user/:id', function(){});1212- app.get('/user/:id/:op?', function(){});1213-1214- We can utilize this lookup functionality to check which routes1215- have been defined, which can be extremely useful for higher level1216- frameworks built on Express.1217-1218- app.lookup.get('/user/:id');1219- // => [Function]1220-1221- app.lookup.get('/user/:id/:op?');1222- // => [Function]1223-1224- app.lookup.put('/user/:id');1225- // => [Function]1226-1227- app.lookup.all('/user/:id');1228- // => [Function, Function]1229-1230- app.lookup.all('/hey');1231- // => []1232-1233- To alias _app.lookup.VERB()_, we can simply invoke _app.VERB()_1234- without a callback, as a shortcut, for example the following are1235- equivalent:1236- 1237- app.lookup.get('/user');1238- app.get('/user');1239-1240- Each function returned has the following properties:1241- 1242- var fn = app.get('/user/:id/:op?')[0];1243-1244- fn.regexp1245- // => /^\/user\/(?:([^\/]+?))(?:\/([^\/]+?))?\/?$/i1246-1247- fn.keys1248- // => ['id', 'op']1249-1250- fn.path1251- // => '/user/:id/:op?'1252-1253- fn.method1254- // => 'GET'1255-1256-### app.match1257-1258- The _app.match_ http methods return an array of callback functions1259- which match the given _url_, which may include a query string etc. This1260- is useful when you want reflect on which routes have the opportunity to1261- respond.1262-1263- Suppose we define the following routes:1264-1265- app.get('/user/:id', function(){});1266- app.put('/user/:id', function(){});1267- app.get('/user/:id/:op?', function(){});1268-1269- Our match against __GET__ will return two functions, since the _:op_1270- in our second route is optional.1271-1272- app.match.get('/user/1');1273- // => [Function, Function]1274-1275- This second call returns only the callback for _/user/:id/:op?_.1276-1277- app.match.get('/user/23/edit');1278- // => [Function]1279-1280- We can also use _all()_ to disregard the http method:1281-1282- app.match.all('/user/20');1283- // => [Function, Function, Function]1284-1285- Each function matched has the following properties:1286- 1287- var fn = app.match.get('/user/23/edit')[0];1288-1289- fn.keys1290- // => ['id', 'op']1291-1292- fn.params1293- // => { id: '23', op: 'edit' }1294-1295- fn.method1296- // => 'GET'1297-1298-### app.mounted(fn)1299-1300-Assign a callback _fn_ which is called when this _Server_ is passed to _Server#use()_.1301-1302- var app = express.createServer(),1303- blog = express.createServer();1304- 1305- blog.mounted(function(parent){1306- // parent is app1307- // "this" is blog1308- });1309- 1310- app.use(blog);1311-1312-### app.register(ext, exports)1313-1314-Register the given template engine _exports_1315-as _ext_. For example we may wish to map ".html"1316-files to jade:1317-1318- app.register('.html', require('jade'));1319-1320-This is also useful for libraries that may not1321-match extensions correctly. For example my haml.js1322-library is installed from npm as "hamljs" so instead1323-of layout.hamljs, we can register the engine as ".haml":1324-1325- app.register('.haml', require('haml-js'));1326-1327-For engines that do not comply with the Express1328-specification, we can also wrap their api this way. Below1329-we map _.md_ to render markdown files, rendering the html once1330-since it will not change on subsequent calls, and support local substitution1331-in the form of "{name}".1332-1333- app.register('.md', {1334- compile: function(str, options){1335- var html = md.toHTML(str);1336- return function(locals){1337- return html.replace(/\{([^}]+)\}/g, function(_, name){1338- return locals[name];1339- });1340- };1341- }1342- });1343-1344-### app.listen([port[, host]])1345-1346-Bind the app server to the given _port_, which defaults to 3000. When _host_ is omitted all1347-connections will be accepted via _INADDR_ANY_.1348-1349- app.listen();1350- app.listen(3000);1351- app.listen(3000, 'n.n.n.n');1352-1353-The _port_ argument may also be a string representing the path to a unix domain socket:1354-1355- app.listen('/tmp/express.sock');1356-1357-Then try it out:1358-1359- $ telnet /tmp/express.sock1360- GET / HTTP/1.11361-1362- HTTP/1.1 200 OK1363- Content-Type: text/plain1364- Content-Length: 111365- 1366- Hello Worlddeleted:docs/index.html-266 removed
1-<html>2- <head>3- <title>Express - node web framework</title>4- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">5- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>6- <style>7- #tagline {8- margin-left: 75px;9- margin-bottom: 30px;10- color: rgba(255,255,255,0.7); }11- html {12- background: #1c1c1c url(images/bg.tile.jpg); }13-14- body {15- margin: 0;16- padding-bottom: 30px;17- font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";18- font-size: 14px;19- line-height: 1.5;20- -webkit-font-smoothing: antialiased;21- background: url(images/bg.jpg) 50% 0 no-repeat;22- color: #8b8b8b; }23-24- * {25- outline: none; }26-27- em {28- color: white; }29-30- a img {31- border: none !important; }32-33- a {34- font-weight: bold;35- text-decoration: none;36- color: white;37- -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;38- -webkit-transition-duration: 0.15s;39- -webkit-transition-timing-function: ease-out; }40- a:hover {41- opacity: 0.8; }42-43- h1, h2, h3, h4 {44- margin: 45px 0 0 0;45- color: white;46- text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }47-48- h3 {49- font-size: 18px; }50- h4 {51- margin-left: 10px;52- font-size: 14px;53- }54-55- pre {56- margin: 20px 10px;57- padding: 25px 20px;58- background: rgba(0,0,0,0.5);59- border: 1px solid #323232;60- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);62- -webkit-border-radius: 5px;63- -moz-border-radius: 5px; }64-65- code {66- font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }67-68- ul {69- margin: 15px 0;70- padding: 0 0 0 35px; }71- ul li {72- margin: 0;73- padding: 2px 0;74- list-style: square; }75- ul li ul {76- margin: 0;77- padding-left: 12px;78- }79-80- .man-name, #Express { display:none; }81-82- .sect {83- margin-left: 40px; }84- img {85- margin-left: 20px;86- margin-bottom: 15px;87- }88-89- #logo {90- display: block;91- margin-left: 30%;92- margin-bottom: 30px;93- width: 194px;94- height: 51px;95- background: url(images/logo.png) 0 0 no-repeat;96- text-indent: -99999px; }97- #logo:hover {98- opacity: 0.7; }99- #logo:active {100- opacity: 0.3; }101-102- #ribbon {103- position: fixed;104- top: 0;105- right: 0;106- z-index: 2; }107-108- #wrapper {109- width: 100%;110- min-height: 800px;111- background: url(images/top.png) 0 0 repeat-x; }112-113- #container {114- margin: 0 auto;115- padding-top: 80px;116- width: 550px; }117-118- #toc {119- position: fixed;120- top: 0;121- left: 0;122- margin: 0 0 0 15px;123- padding: 15px;124- height: 100%;125- background: rgba(0,0,0,0.2);126- overflow: auto;127- border-right: 1px solid rgba(255,255,255,0.05);128- }129- #toc li {130- padding: 0;131- list-style: none;132- }133- #toc li a {134- font-size: 11px;135- }136- #menu {137- margin-left: 75px;138- padding: 0;139- padding-bottom: 30px; }140- #menu li {141- display: inline;142- list-style: none; }143- #menu li a {144- display: block;145- float: left;146- margin: 0 2px;147- padding: 3px 15px;148- background: rgba(0,0,0,0.2);149- -webkit-border-radius: 8px;150- -moz-border-radius: 8px;151- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);153- -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;154- -webkit-transition-duration: 0.15s;155- -webkit-transition-timing-function: ease-out; }156- #menu li a:hover,157- #menu li a.active {158- background: rgba(0,0,0,0.5); }159- #menu li a:active {160- background: rgba(0,0,0,0.1);161- -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);162- -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }163- </style>164- <script>165- $(function(){166- $('.section').hide();167- $('.toggle, a.section-title').toggle(function(){168- $(this).siblings('ul').fadeIn(300);169- return false;170- }, function(){171- $(this).siblings('ul').fadeOut(300);172- return false;173- });174- });175- </script>176- </head>177- <body>178- <a href='http://github.com/visionmedia/express'> 179- <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 180- </a>181- <div id="wrapper">182- <div id="container">183- <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 184- <p id="tagline">185- High performance, high class web development for186- <a href="http://nodejs.org">Node.js</a>187- </p>188- <ul id="menu">189- <li><a href="index.html">Home</a></li>190- <li><a href="guide.html">Guide</a></li>191- <li><a href="screencasts.html">Screencasts</a></li>192- <li><a href="applications.html">Applications</a></li>193- </ul>194-<pre><code>var app = express.createServer();195-196-app.get('/', function(req, res){197- res.send('Hello World');198-});199-200-app.listen(3000);201-</code></pre>202-203-<h2>Features</h2>204-205-<ul>206-<li>Robust routing</li>207-<li>Redirection helpers</li>208-<li>Dynamic view helpers</li>209-<li>Application level view options</li>210-<li>Content negotiation</li>211-<li>Application mounting</li>212-<li>Focus on high performance</li>213-<li>View rendering and partials support</li>214-<li>Environment based configuration</li>215-<li>Session based flash notifications</li>216-<li>Built on <a href="http://github.com/senchalabs/connect">Connect</a></li>217-<li><a href="executable.html">Executable</a> for generating applications quickly</li>218-<li>High test coverage</li>219-</ul>220-221-222-<h2>Contributors</h2>223-224-<p>The following are the major contributors of Express (in no specific order).</p>225-226-<ul>227-<li>TJ Holowaychuk (<a href="http://github.com/visionmedia">visionmedia</a>)</li>228-<li>Ciaran Jessup (<a href="http://github.com/ciaranj">ciaranj</a>)</li>229-<li>Aaron Heckmann (<a href="http://github.com/aheckmann">aheckmann</a>)</li>230-<li>Guillermo Rauch (<a href="http://github.com/guille">guille</a>)</li>231-</ul>232-233-234-<h2>Third-Party Modules</h2>235-236-<p>The following modules compliment or extend Express directly:</p>237-238-<ul>239-<li><a href="http://github.com/visionmedia/express-resource">express-resource</a> provides resourceful routing</li>240-<li><a href="http://github.com/visionmedia/express-messages">express-messages</a> flash message notification rendering</li>241-<li><a href="http://github.com/visionmedia/express-configuration">express-configure</a> async configuration support (load settings from redis etc)</li>242-<li><a href="http://github.com/visionmedia/express-namespace">express-namespace</a> namespaced routing support</li>243-<li><a href="http://github.com/visionmedia/express-expose">express-expose</a> expose objects, functions, modules and more to client-side js</li>244-<li><a href="https://github.com/visionmedia/express-params">express-params</a> app.param() extensions</li>245-<li><a href="https://github.com/LearnBoost/express-mongoose">express-mongoose</a> plugin for easy rendering of Mongoose async Query results</li>246-</ul>247-248-249-<h2>More Information</h2>250-251-<ul>252-<li>#express on freenode</li>253-<li>Follow <a href="http://twitter.com/tjholowaychuk">tjholowaychuk</a> on twitter for updates</li>254-<li><a href="http://groups.google.com/group/express-js">Google Group</a> for discussion</li>255-<li>Visit the <a href="http://github.com/visionmedia/express/wiki">Wiki</a></li>256-<li><a href="http://hideyukisaito.com/doc/expressjs/">日本語ドキュメンテーション</a> by <a href="https://github.com/hideyukisaito">hideyukisaito</a></li>257-<li>Screencast – <a href="http://bit.ly/eRYu0O">Introduction</a></li>258-<li>Screencast – <a href="http://bit.ly/dU13Fx">View Partials</a></li>259-<li>Screencast – <a href="http://bit.ly/hX4IaH">Route Specific Middleware</a></li>260-<li>Screencast – <a href="http://bit.ly/eNqmVs">Route Path Placeholder Preconditions</a></li>261-</ul>262-263- </div>264- </div>265- </body>266-</html>deleted:docs/index.md-54 removed
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
var app = express.createServer(); app.get('/', function(req, res){ res.send('Hello World'); });
app.listen(3000);
## Features
* Robust routing * Redirection helpers * Dynamic view helpers * Application level view options * Content negotiation * Application mounting * Focus on high performance * View rendering and partials support * Environment based configuration * Session based flash notifications * Built on [Connect](http://github.com/senchalabs/connect) * [Executable](executable.html) for generating applications quickly * High test coverage
## Contributors
The following are the major contributors of Express (in no specific order).
* TJ Holowaychuk ([visionmedia](http://github.com/visionmedia)) * Ciaran Jessup ([ciaranj](http://github.com/ciaranj)) * Aaron Heckmann ([aheckmann](http://github.com/aheckmann)) * Guillermo Rauch ([guille](http://github.com/guille))
## Third-Party Modules
The following modules compliment or extend Express directly:
* [express-resource](http://github.com/visionmedia/express-resource) provides resourceful routing * [express-messages](http://github.com/visionmedia/express-messages) flash message notification rendering * [express-configure](http://github.com/visionmedia/express-configuration) async configuration support (load settings from redis etc) * [express-namespace](http://github.com/visionmedia/express-namespace) namespaced routing support * [express-expose](http://github.com/visionmedia/express-expose) expose objects, functions, modules and more to client-side js * [express-params](https://github.com/visionmedia/express-params) app.param() extensions * [express-mongoose](https://github.com/LearnBoost/express-mongoose) plugin for easy rendering of Mongoose async Query results
## More Information
* \#express on freenode * Follow [tjholowaychuk](http://twitter.com/tjholowaychuk) on twitter for updates * [Google Group](http://groups.google.com/group/express-js) for discussion * Visit the [Wiki](http://github.com/visionmedia/express/wiki) * [日本語ドキュメンテーション](http://hideyukisaito.com/doc/expressjs/) by [hideyukisaito](https://github.com/hideyukisaito) * [Русскоязычная документация](http://express-js.ru/)deleted:docs/migrate.html-394 removed
1-<html>2- <head>3- <title>Express - node web framework</title>4- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">5- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>6- <style>7- #tagline {8- margin-left: 75px;9- margin-bottom: 30px;10- color: rgba(255,255,255,0.7); }11- html {12- background: #1c1c1c url(images/bg.tile.jpg); }13-14- body {15- margin: 0;16- padding-bottom: 30px;17- font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";18- font-size: 14px;19- line-height: 1.5;20- -webkit-font-smoothing: antialiased;21- background: url(images/bg.jpg) 50% 0 no-repeat;22- color: #8b8b8b; }23-24- * {25- outline: none; }26-27- em {28- color: white; }29-30- a img {31- border: none !important; }32-33- a {34- font-weight: bold;35- text-decoration: none;36- color: white;37- -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;38- -webkit-transition-duration: 0.15s;39- -webkit-transition-timing-function: ease-out; }40- a:hover {41- opacity: 0.8; }42-43- h1, h2, h3, h4 {44- margin: 45px 0 0 0;45- color: white;46- text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }47-48- h3 {49- font-size: 18px; }50- h4 {51- margin-left: 10px;52- font-size: 14px;53- }54-55- pre {56- margin: 20px 10px;57- padding: 25px 20px;58- background: rgba(0,0,0,0.5);59- border: 1px solid #323232;60- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);62- -webkit-border-radius: 5px;63- -moz-border-radius: 5px; }64-65- code {66- font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }67-68- ul {69- margin: 15px 0;70- padding: 0 0 0 35px; }71- ul li {72- margin: 0;73- padding: 2px 0;74- list-style: square; }75- ul li ul {76- margin: 0;77- padding-left: 12px;78- }79-80- .man-name, #Express { display:none; }81-82- .sect {83- margin-left: 40px; }84- img {85- margin-left: 20px;86- margin-bottom: 15px;87- }88-89- #logo {90- display: block;91- margin-left: 30%;92- margin-bottom: 30px;93- width: 194px;94- height: 51px;95- background: url(images/logo.png) 0 0 no-repeat;96- text-indent: -99999px; }97- #logo:hover {98- opacity: 0.7; }99- #logo:active {100- opacity: 0.3; }101-102- #ribbon {103- position: fixed;104- top: 0;105- right: 0;106- z-index: 2; }107-108- #wrapper {109- width: 100%;110- min-height: 800px;111- background: url(images/top.png) 0 0 repeat-x; }112-113- #container {114- margin: 0 auto;115- padding-top: 80px;116- width: 550px; }117-118- #toc {119- position: fixed;120- top: 0;121- left: 0;122- margin: 0 0 0 15px;123- padding: 15px;124- height: 100%;125- background: rgba(0,0,0,0.2);126- overflow: auto;127- border-right: 1px solid rgba(255,255,255,0.05);128- }129- #toc li {130- padding: 0;131- list-style: none;132- }133- #toc li a {134- font-size: 11px;135- }136- #menu {137- margin-left: 75px;138- padding: 0;139- padding-bottom: 30px; }140- #menu li {141- display: inline;142- list-style: none; }143- #menu li a {144- display: block;145- float: left;146- margin: 0 2px;147- padding: 3px 15px;148- background: rgba(0,0,0,0.2);149- -webkit-border-radius: 8px;150- -moz-border-radius: 8px;151- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);153- -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;154- -webkit-transition-duration: 0.15s;155- -webkit-transition-timing-function: ease-out; }156- #menu li a:hover,157- #menu li a.active {158- background: rgba(0,0,0,0.5); }159- #menu li a:active {160- background: rgba(0,0,0,0.1);161- -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);162- -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }163- </style>164- <script>165- $(function(){166- $('.section').hide();167- $('.toggle, a.section-title').toggle(function(){168- $(this).siblings('ul').fadeIn(300);169- return false;170- }, function(){171- $(this).siblings('ul').fadeOut(300);172- return false;173- });174- });175- </script>176- </head>177- <body>178- <a href='http://github.com/visionmedia/express'> 179- <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 180- </a>181- <div id="wrapper">182- <div id="container">183- <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 184- <p id="tagline">185- High performance, high class web development for186- <a href="http://nodejs.org">Node.js</a>187- </p>188- <ul id="menu">189- <li><a href="index.html">Home</a></li>190- <li><a href="guide.html">Guide</a></li>191- <li><a href="screencasts.html">Screencasts</a></li>192- <li><a href="applications.html">Applications</a></li>193- </ul>194-<h3>Express 1.x to 2.x Migration</h3>195-196-<h3>HTTPS</h3>197-198-<p> Creating an HTTPS server is simply, simply pass the TLS options to <em>express.createServer()</em>:</p>199-200-<pre><code> var app = express.createServer({201- key: ...202- , cert: ...203- });204-205- app.listen(443);206-</code></pre>207-208-<h3>req.header() Referrer</h3>209-210-<p> Previously if anyone was doing something similar to:</p>211-212-<pre><code> req.headers.referrer || req.headers.referer213- req.header('Referrer') || req.header('Referer')214-</code></pre>215-216-<p> With the new special-case we may now simply use <em>Referrer</em> which will return either if defined:</p>217-218-<pre><code> req.header('Referrer')219-</code></pre>220-221-<h3>res.local(name, val)</h3>222-223-<p> Previously all local variables had to be passed to <em>res.render()</em>, or either <em>app.helpers()</em> or <em>app.dynamicHelpers()</em>, now we may do this at the request-level progressively. The <em>res.local()</em> method accepts a <em>name</em> and <em>val</em>, however the locals passed to <em>res.render()</em> will take precedence.</p>224-225-<p> For example we may utilize this feature to create locals in middleware:</p>226-227-<pre><code> function loadUser(req, res, next) {228- User.get(req.params.id, function(err, user){229- res.local('user', user);230- next();231- });232- }233-234- app.get('/user/:id', loadUser, function(req, res){235- res.render('user');236- });237-</code></pre>238-239-<h3>req.param(name[, defaultValue])</h3>240-241-<p> Previously only <em>name</em> was accepted, so some of you may have been doing the following:</p>242-243-<pre><code> var id = req.param('id') || req.user.id;244-</code></pre>245-246-<p> The new <em>defaultValue</em> argument can handle this nicely:</p>247-248-<pre><code> var id = req.param('id', req.user.id);249-</code></pre>250-251-<h3>app.helpers() / app.locals()</h3>252-253-<p> <em>app.locals()</em> is now an alias of <em>app.helpers()</em>, as helpers makes more sense for functions.</p>254-255-<h3>req.accepts(type)</h3>256-257-<p> <em>req.accepts()</em> now accepts extensions:</p>258-259-<pre><code> // Accept: text/html260- req.accepts('html');261- req.accepts('.html');262- // => true263-264- // Accept: text/*; application/json265- req.accepts('html');266- req.accepts('text/*');267- req.accepts('text/plain');268- req.accepts('application/json');269- // => true270-271- req.accepts('image/png');272- req.accepts('png');273- // => false274-</code></pre>275-276-<h3>res.cookie()</h3>277-278-<p> Previously only directly values could be passed, so for example:</p>279-280-<pre><code>res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000) });281-</code></pre>282-283-<p>However now we have the alternative <em>maxAge</em> property which may be used to set <em>expires</em> relative to <em>Date.now()</em> in milliseconds, so our example above can now become:</p>284-285-<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });286-</code></pre>287-288-<h3>res.download() / res.sendfile()</h3>289-290-<p> Both of these methods now utilize Connect’s static file server behind the scenes (actually the previous Express code was ported to Connect 1.0). With this change comes a change to the callback as well. Previously the <em>path</em> and <em>stream</em> were passed, however now only an <em>error</em> is passed, when no error has occurred the callback will be invoked indicating that the file transfer is complete. The callback remains optional:</p>291-292-<pre><code> res.download('/path/to/file');293-294- res.download('/path/to/file', function(err){295- if (err) {296- console.error(err);297- } else {298- console.log('transferred');299- }300- });301-</code></pre>302-303-<p> The <em>stream threshold</em> setting was removed.</p>304-305-<h3>res.render()</h3>306-307-<p> Previously locals were passed as a separate key:</p>308-309-<pre><code> res.render('user', { layout: false, locals: { user: user }});310-</code></pre>311-312-<p> In Express 2.0 both the locals and the options are one in the same, meaning you cannot have a local variable named <em>layout</em> as it is reserved for express, however this cleans up the API:</p>313-314-<pre><code> res.render('user', { layout: false, user: user });315-</code></pre>316-317-<h3>res.partial()</h3>318-319-<p> Express 2.0 adds the <em>res.partial()</em> method, helpful for rendering partial fragments over WebSockets or Ajax requests etc. The API is identical to the <em>partial()</em> calls within views.</p>320-321-<pre><code> // render a collection of comments322- res.partial('comment', [comment1, comment2]); 323-324- // render a single comment325- res.partial('comment', comment);326-</code></pre>327-328-<h3>partial() locals</h3>329-330-<p> Both <em>res.partial()</em> and the <em>partial()</em> functions accept a single object consisting of both the options and the locals. Previously with Express 1.x you may pass <em>user</em> to a partial, along with <em>date</em> like so:</p>331-332-<pre><code> partial('user', { object: user, locals: { date: new Date }})333-</code></pre>334-335-<p>or perhaps if you preferred not to use the inferred name <em>user</em> you may used a local for this as well:</p>336-337-<pre><code> partial('user', { locals: { user: user, date: new Date }})338-</code></pre>339-340-<p> With recent changes to Express 2.x the object passed is now both, so the following is valid for the <em>object</em> option and locals:</p>341-342-<pre><code> partial('user', { object: user, date: new Date })343-</code></pre>344-345-<p> Or the following which is equivalent, however the local var name is explicitly set to <em>user</em> instead of deduced from the filename.</p>346-347-<pre><code> partial('user', { user: user, date: new Date })348-</code></pre>349-350-<p> When a “basic” object aka <em>{}</em> or <em>new Object</em> is passed, it is considered options, otherwise it is considered the <em>object</em>. The following are equivalent:</p>351-352-<pre><code> partial('user', user); 353- partial('user', { object: user }); 354-</code></pre>355-356-<h3>Template Engine Compliance</h3>357-358-<p> To comply with Express previously engines needed the following signature:</p>359-360-<pre><code> engine.render(str, options, function(err){});361-</code></pre>362-363-<p> Now they must export a <em>compile()</em> function, returning a function which when called with local variables will render the template. This allows Express to cache the compiled function in memory during production.</p>364-365-<pre><code> var fn = engine.compile(str, options);366- fn(locals);367-</code></pre>368-369-<h3>View Partial Lookup</h3>370-371-<p> Previously partials were loaded relative to the now removed <em>view partials</em> directory setting, or by default <em>views/partials</em>, now they are relative to the view calling them, read more on <a href="guide.html#view-lookup">view lookup</a>.</p>372-373-<h3>Mime Types</h3>374-375-<p> Express and Connect now utilize the <em>mime</em> module in npm, so to add more use:</p>376-377-<pre><code> require('mime').define({ 'foo/bar': ['foo', 'bar'] });378-</code></pre>379-380-<h3>static() middleware</h3>381-382-<p> Previously named <code>staticProvider()</code>, the now <code>static()</code> middleware takes a single directory path, followed by options.</p>383-384-<pre><code> app.use(express.static(__dirname + '/public', { maxAge: oneYear }));385-</code></pre>386-387-<p>Previously when using options the <code>root</code> option would be used for this:</p>388-389-<pre><code> app.use(express.staticProvider({ root: __dirname + '/public', maxAge: oneYear }));390-</code></pre>391- </div>392- </div>393- </body>394-</html>deleted:docs/migrate.md-177 removed
1-2-### Express 1.x to 2.x Migration3-4-### HTTPS5-6- Creating an HTTPS server is simply, simply pass the TLS options to _express.createServer()_:7- 8- var app = express.createServer({9- key: ...10- , cert: ...11- });12-13- app.listen(443);14-15-### req.header() Referrer16-17- Previously if anyone was doing something similar to:18- 19- req.headers.referrer || req.headers.referer20- req.header('Referrer') || req.header('Referer')21-22- With the new special-case we may now simply use _Referrer_ which will return either if defined:23- 24- req.header('Referrer')25-26-### res.local(name, val)27-28- Previously all local variables had to be passed to _res.render()_, or either _app.helpers()_ or _app.dynamicHelpers()_, now we may do this at the request-level progressively. The _res.local()_ method accepts a _name_ and _val_, however the locals passed to _res.render()_ will take precedence.29-30- For example we may utilize this feature to create locals in middleware:31-32- function loadUser(req, res, next) {33- User.get(req.params.id, function(err, user){34- res.local('user', user);35- next();36- });37- }38-39- app.get('/user/:id', loadUser, function(req, res){40- res.render('user');41- });42-43-### req.param(name[, defaultValue])44-45- Previously only _name_ was accepted, so some of you may have been doing the following:46- 47- var id = req.param('id') || req.user.id;48-49- The new _defaultValue_ argument can handle this nicely:50- 51- var id = req.param('id', req.user.id);52-53-### app.helpers() / app.locals()54-55- _app.locals()_ is now an alias of _app.helpers()_, as helpers makes more sense for functions.56-57-### req.accepts(type)58-59- _req.accepts()_ now accepts extensions:60- 61- 62- // Accept: text/html63- req.accepts('html');64- req.accepts('.html');65- // => true66- 67- // Accept: text/*; application/json68- req.accepts('html');69- req.accepts('text/*');70- req.accepts('text/plain');71- req.accepts('application/json');72- // => true73- 74- req.accepts('image/png');75- req.accepts('png');76- // => false77-78-### res.cookie()79-80- Previously only directly values could be passed, so for example:81-82- res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000) });83-84-However now we have the alternative _maxAge_ property which may be used to set _expires_ relative to _Date.now()_ in milliseconds, so our example above can now become:85-86- res.cookie('rememberme', 'yes', { maxAge: 900000 });87-88-### res.download() / res.sendfile()89-90- Both of these methods now utilize Connect's static file server behind the scenes (actually the previous Express code was ported to Connect 1.0). With this change comes a change to the callback as well. Previously the _path_ and _stream_ were passed, however now only an _error_ is passed, when no error has occurred the callback will be invoked indicating that the file transfer is complete. The callback remains optional:91- 92- res.download('/path/to/file');93-94- res.download('/path/to/file', function(err){95- if (err) {96- console.error(err);97- } else {98- console.log('transferred');99- }100- });101-102- The _stream threshold_ setting was removed.103-104-### res.render()105-106- Previously locals were passed as a separate key:107- 108- res.render('user', { layout: false, locals: { user: user }});109-110- In Express 2.0 both the locals and the options are one in the same, meaning you cannot have a local variable named _layout_ as it is reserved for express, however this cleans up the API:111- 112- res.render('user', { layout: false, user: user });113-114-### res.partial()115-116- Express 2.0 adds the _res.partial()_ method, helpful for rendering partial fragments over WebSockets or Ajax requests etc. The API is identical to the _partial()_ calls within views.117- 118- // render a collection of comments119- res.partial('comment', [comment1, comment2]); 120-121- // render a single comment122- res.partial('comment', comment);123-124-### partial() locals125-126- Both _res.partial()_ and the _partial()_ functions accept a single object consisting of both the options and the locals. Previously with Express 1.x you may pass _user_ to a partial, along with _date_ like so:127- 128- partial('user', { object: user, locals: { date: new Date }})129-130-or perhaps if you preferred not to use the inferred name _user_ you may used a local for this as well:131-132- partial('user', { locals: { user: user, date: new Date }})133-134- With recent changes to Express 2.x the object passed is now both, so the following is valid for the _object_ option and locals:135- 136- partial('user', { object: user, date: new Date })137-138- Or the following which is equivalent, however the local var name is explicitly set to _user_ instead of deduced from the filename.139- 140- partial('user', { user: user, date: new Date })141-142- When a "basic" object aka _{}_ or _new Object_ is passed, it is considered options, otherwise it is considered the _object_. The following are equivalent:143- 144- partial('user', user); 145- partial('user', { object: user }); 146-147-### Template Engine Compliance148-149- To comply with Express previously engines needed the following signature:150- 151- engine.render(str, options, function(err){});152-153- Now they must export a _compile()_ function, returning a function which when called with local variables will render the template. This allows Express to cache the compiled function in memory during production.154- 155- var fn = engine.compile(str, options);156- fn(locals);157-158-### View Partial Lookup159-160- Previously partials were loaded relative to the now removed _view partials_ directory setting, or by default _views/partials_, now they are relative to the view calling them, read more on [view lookup](guide.html#view-lookup).161-162-### Mime Types163-164- Express and Connect now utilize the _mime_ module in npm, so to add more use:165- 166- require('mime').define({ 'foo/bar': ['foo', 'bar'] });167-168-### static() middleware169-170- Previously named `staticProvider()`, the now `static()` middleware takes a single directory path, followed by options.171- 172- app.use(express.static(__dirname + '/public', { maxAge: oneYear }));173-174-Previously when using options the `root` option would be used for this:175-176- app.use(express.staticProvider({ root: __dirname + '/public', maxAge: oneYear }));177-deleted:docs/screencasts.html-224 removed
1-<html>2- <head>3- <title>Express - node web framework</title>4- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">5- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>6- <style>7- #tagline {8- margin-left: 75px;9- margin-bottom: 30px;10- color: rgba(255,255,255,0.7); }11- html {12- background: #1c1c1c url(images/bg.tile.jpg); }13-14- body {15- margin: 0;16- padding-bottom: 30px;17- font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";18- font-size: 14px;19- line-height: 1.5;20- -webkit-font-smoothing: antialiased;21- background: url(images/bg.jpg) 50% 0 no-repeat;22- color: #8b8b8b; }23-24- * {25- outline: none; }26-27- em {28- color: white; }29-30- a img {31- border: none !important; }32-33- a {34- font-weight: bold;35- text-decoration: none;36- color: white;37- -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;38- -webkit-transition-duration: 0.15s;39- -webkit-transition-timing-function: ease-out; }40- a:hover {41- opacity: 0.8; }42-43- h1, h2, h3, h4 {44- margin: 45px 0 0 0;45- color: white;46- text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }47-48- h3 {49- font-size: 18px; }50- h4 {51- margin-left: 10px;52- font-size: 14px;53- }54-55- pre {56- margin: 20px 10px;57- padding: 25px 20px;58- background: rgba(0,0,0,0.5);59- border: 1px solid #323232;60- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);62- -webkit-border-radius: 5px;63- -moz-border-radius: 5px; }64-65- code {66- font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }67-68- ul {69- margin: 15px 0;70- padding: 0 0 0 35px; }71- ul li {72- margin: 0;73- padding: 2px 0;74- list-style: square; }75- ul li ul {76- margin: 0;77- padding-left: 12px;78- }79-80- .man-name, #Express { display:none; }81-82- .sect {83- margin-left: 40px; }84- img {85- margin-left: 20px;86- margin-bottom: 15px;87- }88-89- #logo {90- display: block;91- margin-left: 30%;92- margin-bottom: 30px;93- width: 194px;94- height: 51px;95- background: url(images/logo.png) 0 0 no-repeat;96- text-indent: -99999px; }97- #logo:hover {98- opacity: 0.7; }99- #logo:active {100- opacity: 0.3; }101-102- #ribbon {103- position: fixed;104- top: 0;105- right: 0;106- z-index: 2; }107-108- #wrapper {109- width: 100%;110- min-height: 800px;111- background: url(images/top.png) 0 0 repeat-x; }112-113- #container {114- margin: 0 auto;115- padding-top: 80px;116- width: 550px; }117-118- #toc {119- position: fixed;120- top: 0;121- left: 0;122- margin: 0 0 0 15px;123- padding: 15px;124- height: 100%;125- background: rgba(0,0,0,0.2);126- overflow: auto;127- border-right: 1px solid rgba(255,255,255,0.05);128- }129- #toc li {130- padding: 0;131- list-style: none;132- }133- #toc li a {134- font-size: 11px;135- }136- #menu {137- margin-left: 75px;138- padding: 0;139- padding-bottom: 30px; }140- #menu li {141- display: inline;142- list-style: none; }143- #menu li a {144- display: block;145- float: left;146- margin: 0 2px;147- padding: 3px 15px;148- background: rgba(0,0,0,0.2);149- -webkit-border-radius: 8px;150- -moz-border-radius: 8px;151- -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152- -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);153- -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;154- -webkit-transition-duration: 0.15s;155- -webkit-transition-timing-function: ease-out; }156- #menu li a:hover,157- #menu li a.active {158- background: rgba(0,0,0,0.5); }159- #menu li a:active {160- background: rgba(0,0,0,0.1);161- -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);162- -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }163- </style>164- <script>165- $(function(){166- $('.section').hide();167- $('.toggle, a.section-title').toggle(function(){168- $(this).siblings('ul').fadeIn(300);169- return false;170- }, function(){171- $(this).siblings('ul').fadeOut(300);172- return false;173- });174- });175- </script>176- </head>177- <body>178- <a href='http://github.com/visionmedia/express'> 179- <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 180- </a>181- <div id="wrapper">182- <div id="container">183- <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 184- <p id="tagline">185- High performance, high class web development for186- <a href="http://nodejs.org">Node.js</a>187- </p>188- <ul id="menu">189- <li><a href="index.html">Home</a></li>190- <li><a href="guide.html">Guide</a></li>191- <li><a href="screencasts.html">Screencasts</a></li>192- <li><a href="applications.html">Applications</a></li>193- </ul>194-<h3>Introduction</h3>195-196-<p>This introduction screencast covers the basics of Express, and how to get started with your first application.</p>197-198-<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=139583' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=139583' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>199-200-201-<h3>View Partials</h3>202-203-<p>In this screencast we work with partials to display a collection of users using the <a href="http://jade-lang.com">Jade</a> template engine, and learn about view path resolution.</p>204-205-<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=139591' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=139591' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>206-207-208-<h3>Route Specific Middleware</h3>209-210-<p>In the screencast below we learn about the benefits of route-specific middleware.</p>211-212-<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=140296' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=140296' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>213-214-215-<h3>Route Param Preconditions</h3>216-217-<p>Learn about route parameter (<em>/user/:id</em>) pre-conditions, providing automated validation, and loading of data via the named route param segments.</p>218-219-<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=140300' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=140300' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>220-221- </div>222- </div>223- </body>224-</html>deleted:docs/screencasts.md-24 removed
123456789101112131415161718192021222324
### Introduction
This introduction screencast covers the basics of Express, and how to get started with your first application.
<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=139583' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=139583' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>
### View Partials
In this screencast we work with partials to display a collection of users using the [Jade](http://jade-lang.com) template engine, and learn about view path resolution.
<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=139591' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=139591' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>
### Route Specific Middleware
In the screencast below we learn about the benefits of route-specific middleware.
<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=140296' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=140296' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>
### Route Param Preconditions
Learn about route parameter (_/user/:id_) pre-conditions, providing automated validation, and loading of data via the named route param segments.
<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0' width='560' height='345'><param name='movie' value='http://screenr.com/Content/assets/screenr_1116090935.swf' /><param name='flashvars' value='i=140300' /><param name='allowFullScreen' value='true' /><embed src='http://screenr.com/Content/assets/screenr_1116090935.swf' flashvars='i=140300' allowFullScreen='true' width='560' height='345' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed></object>No newline at end of file