Skip to content
Myr

migrate.html

This file

Path

docs/migrate.html

394 lines · 11.8 KiB · HTML

Last changed by

fixed docs charsetTj Holowaychukbefore adoptionfe6c583

History of this file

Take it

The walk stopped after 200 commits: lines older than fae1ba9 point at it.

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 1–3why, one step back
1<html>
2 <head>3 <title>Express - node web framework</title>

fixed docs charsetTj Holowaychukbefore adoptionfe6c583

line 4why, one step back
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 5–193why, one step back
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); }1314 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; }2324 * {25 outline: none; }2627 em {28 color: white; }2930 a img {31 border: none !important; }3233 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; }4243 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); }4748 h3 {49 font-size: 18px; }50 h4 {51 margin-left: 10px;52 font-size: 14px;53 }5455 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; }6465 code {66 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }6768 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 }7980 .man-name, #Express { display:none; }8182 .sect {83 margin-left: 40px; }84 img {85 margin-left: 20px;86 margin-bottom: 15px;87 }8889 #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; }101102 #ribbon {103 position: fixed;104 top: 0;105 right: 0;106 z-index: 2; }107108 #wrapper {109 width: 100%;110 min-height: 800px;111 background: url(images/top.png) 0 0 repeat-x; }112113 #container {114 margin: 0 auto;115 padding-top: 80px;116 width: 550px; }117118 #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>

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 194why, one step back
194<h3>Express 1.x to 2.x Migration</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 195why, one step back
195

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 196why, one step back
196<h3>HTTPS</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 197–204why, one step back
197
198<p> Creating an HTTPS server is simply, simply pass the TLS options to <em>express.createServer()</em>:</p>199200<pre><code> var app = express.createServer({201 key: ...202 , cert: ...203 });204

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 205–206why, one step back
205 app.listen(443);
206</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 207why, one step back
207

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 208why, one step back
208<h3>req.header() Referrer</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 209–212why, one step back
209
210<p> Previously if anyone was doing something similar to:</p>211212<pre><code> req.headers.referrer || req.headers.referer

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 213–214why, one step back
213 req.header('Referrer') || req.header('Referer')
214</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 215–217why, one step back
215
216<p> With the new special-case we may now simply use <em>Referrer</em> which will return either if defined:</p>217

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 218–219why, one step back
218<pre><code> req.header('Referrer')
219</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 220why, one step back
220

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 221why, one step back
221<h3>res.local(name, val)</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 222–235why, one step back
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>224225<p> For example we may utilize this feature to create locals in middleware:</p>226227<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 }233234 app.get('/user/:id', loadUser, function(req, res){235 res.render('user');

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 236–237why, one step back
236 });
237</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 238why, one step back
238

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 239why, one step back
239<h3>req.param(name[, defaultValue])</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 240–242why, one step back
240
241<p> Previously only <em>name</em> was accepted, so some of you may have been doing the following:</p>242

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 243–244why, one step back
243<pre><code> var id = req.param('id') || req.user.id;
244</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 245–247why, one step back
245
246<p> The new <em>defaultValue</em> argument can handle this nicely:</p>247

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 248–249why, one step back
248<pre><code> var id = req.param('id', req.user.id);
249</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 250why, one step back
250

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 251why, one step back
251<h3>app.helpers() / app.locals()</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 252–254why, one step back
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

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 255why, one step back
255<h3>req.accepts(type)</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 256–272why, one step back
256
257<p> <em>req.accepts()</em> now accepts extensions:</p>258259<pre><code> // Accept: text/html260 req.accepts('html');261 req.accepts('.html');262 // =&gt; true263264 // Accept: text/*; application/json265 req.accepts('html');266 req.accepts('text/*');267 req.accepts('text/plain');268 req.accepts('application/json');269 // =&gt; true270271 req.accepts('image/png');272 req.accepts('png');

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 273–274why, one step back
273 // =&gt; false
274</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 275why, one step back
275

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 276why, one step back
276<h3>res.cookie()</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 277–279why, one step back
277
278<p> Previously only directly values could be passed, so for example:</p>279

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 280–281why, one step back
280<pre><code>res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000) });
281</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 282–284why, one step back
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

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 285–286why, one step back
285<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });
286</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 287why, one step back
287

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 288why, one step back
288<h3>res.download() / res.sendfile()</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 289why, one step back
289

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 290why, one step back
290<p> Both of these methods now utilize Connect&rsquo;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>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 291–299why, one step back
291
292<pre><code> res.download('/path/to/file');293294 res.download('/path/to/file', function(err){295 if (err) {296 console.error(err);297 } else {298 console.log('transferred');299 }

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 300–301why, one step back
300 });
301</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 302–304why, one step back
302
303<p> The <em>stream threshold</em> setting was removed.</p>304

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 305why, one step back
305<h3>res.render()</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 306–308why, one step back
306
307<p> Previously locals were passed as a separate key:</p>308

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 309–310why, one step back
309<pre><code> res.render('user', { layout: false, locals: { user: user }});
310</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 311–313why, one step back
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

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 314–315why, one step back
314<pre><code> res.render('user', { layout: false, user: user });
315</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 316why, one step back
316

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 317why, one step back
317<h3>res.partial()</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 318–324why, one step back
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>320321<pre><code> // render a collection of comments322 res.partial('comment', [comment1, comment2]); 323324 // render a single comment

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 325–326why, one step back
325 res.partial('comment', comment);
326</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 327why, one step back
327

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 328why, one step back
328<h3>partial() locals</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 329why, one step back
329

updated docsTj Holowaychukbefore adoption652e166

line 330why, one step back
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>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 331why, one step back
331

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 332–333why, one step back
332<pre><code> partial('user', { object: user, locals: { date: new Date }})
333</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 334–336why, one step back
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

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 337–338why, one step back
337<pre><code> partial('user', { locals: { user: user, date: new Date }})
338</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 339–341why, one step back
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

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 342–343why, one step back
342<pre><code> partial('user', { object: user, date: new Date })
343</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 344–346why, one step back
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

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 347–348why, one step back
347<pre><code> partial('user', { user: user, date: new Date })
348</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 349why, one step back
349

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 350why, one step back
350<p> When a &ldquo;basic&rdquo; 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>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 351–352why, one step back
351
352<pre><code> partial('user', user);

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 353–354why, one step back
353 partial('user', { object: user });
354</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 355why, one step back
355

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 356why, one step back
356<h3>Template Engine Compliance</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 357–359why, one step back
357
358<p> To comply with Express previously engines needed the following signature:</p>359

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 360–361why, one step back
360<pre><code> engine.render(str, options, function(err){});
361</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 362–365why, one step back
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>364365<pre><code> var fn = engine.compile(str, options);

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 366–367why, one step back
366 fn(locals);
367</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 368why, one step back
368

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 369why, one step back
369<h3>View Partial Lookup</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 370why, one step back
370

updated docsTj Holowaychukbefore adoption652e166

line 371why, one step back
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>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 372why, one step back
372

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 373why, one step back
373<h3>Mime Types</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 374–376why, one step back
374
375<p> Express and Connect now utilize the <em>mime</em> module in npm, so to add more use:</p>376

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 377–378why, one step back
377<pre><code> require('mime').define({ 'foo/bar': ['foo', 'bar'] });
378</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

line 379why, one step back
379

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 380why, one step back
380<h3>static() middleware</h3>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 381–383why, one step back
381
382<p> Previously named <code>staticProvider()</code>, the now <code>static()</code> middleware takes a single directory path, followed by options.</p>383

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

lines 384–385why, one step back
384<pre><code> app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
385</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 386–389why, one step back
386
387<p>Previously when using options the <code>root</code> option would be used for this:</p>388389<pre><code> app.use(express.staticProvider({ root: __dirname + '/public', maxAge: oneYear }));

fixed docs due to markdown-js not supporting htmlTj Holowaychukbefore adoptiona75e60a

line 390why, one step back
390</code></pre>

doc typoTj Holowaychukbefore adoptionfae1ba9

lines 391–394why, one step back
391 </div>
392 </div>393 </body>394</html>