Skip to content
Myr

migrate.html

This file

Path

docs/migrate.html

393 lines · 11.8 KiB · HTML

Last changed by

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

History of this file

Take it

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

Fixed two testsTj Holowaychukbefore adoptiondf49769

lines 1–192why, one step back
1<html>
2 <head>3 <title>Express - node web framework</title>4 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>5 <style>6 #tagline {7 margin-left: 75px;8 margin-bottom: 30px;9 color: rgba(255,255,255,0.7); }10 html {11 background: #1c1c1c url(images/bg.tile.jpg); }1213 body {14 margin: 0;15 padding-bottom: 30px;16 font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial";17 font-size: 14px;18 line-height: 1.5;19 -webkit-font-smoothing: antialiased;20 background: url(images/bg.jpg) 50% 0 no-repeat;21 color: #8b8b8b; }2223 * {24 outline: none; }2526 em {27 color: white; }2829 a img {30 border: none !important; }3132 a {33 font-weight: bold;34 text-decoration: none;35 color: white;36 -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow;37 -webkit-transition-duration: 0.15s;38 -webkit-transition-timing-function: ease-out; }39 a:hover {40 opacity: 0.8; }4142 h1, h2, h3, h4 {43 margin: 45px 0 0 0;44 color: white;45 text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }4647 h3 {48 font-size: 18px; }49 h4 {50 margin-left: 10px;51 font-size: 14px;52 }5354 pre {55 margin: 20px 10px;56 padding: 25px 20px;57 background: rgba(0,0,0,0.5);58 border: 1px solid #323232;59 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);60 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);61 -webkit-border-radius: 5px;62 -moz-border-radius: 5px; }6364 code {65 font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }6667 ul {68 margin: 15px 0;69 padding: 0 0 0 35px; }70 ul li {71 margin: 0;72 padding: 2px 0;73 list-style: square; }74 ul li ul {75 margin: 0;76 padding-left: 12px;77 }7879 .man-name, #Express { display:none; }8081 .sect {82 margin-left: 40px; }83 img {84 margin-left: 20px;85 margin-bottom: 15px;86 }8788 #logo {89 display: block;90 margin-left: 30%;91 margin-bottom: 30px;92 width: 194px;93 height: 51px;94 background: url(images/logo.png) 0 0 no-repeat;95 text-indent: -99999px; }96 #logo:hover {97 opacity: 0.7; }98 #logo:active {99 opacity: 0.3; }100101 #ribbon {102 position: fixed;103 top: 0;104 right: 0;105 z-index: 2; }106107 #wrapper {108 width: 100%;109 min-height: 800px;110 background: url(images/top.png) 0 0 repeat-x; }111112 #container {113 margin: 0 auto;114 padding-top: 80px;115 width: 550px; }116117 #toc {118 position: fixed;119 top: 0;120 left: 0;121 margin: 0 0 0 15px;122 padding: 15px;123 height: 100%;124 background: rgba(0,0,0,0.2);125 overflow: auto;126 border-right: 1px solid rgba(255,255,255,0.05);127 }128 #toc li {129 padding: 0;130 list-style: none;131 }132 #toc li a {133 font-size: 11px;134 }135 #menu {136 margin-left: 75px;137 padding: 0;138 padding-bottom: 30px; }139 #menu li {140 display: inline;141 list-style: none; }142 #menu li a {143 display: block;144 float: left;145 margin: 0 2px;146 padding: 3px 15px;147 background: rgba(0,0,0,0.2);148 -webkit-border-radius: 8px;149 -moz-border-radius: 8px;150 -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);151 -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6);152 -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow;153 -webkit-transition-duration: 0.15s;154 -webkit-transition-timing-function: ease-out; }155 #menu li a:hover,156 #menu li a.active {157 background: rgba(0,0,0,0.5); }158 #menu li a:active {159 background: rgba(0,0,0,0.1);160 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4);161 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); }162 </style>163 <script>164 $(function(){165 $('.section').hide();166 $('.toggle, a.section-title').toggle(function(){167 $(this).siblings('ul').fadeIn(300);168 return false;169 }, function(){170 $(this).siblings('ul').fadeOut(300);171 return false;172 });173 });174 </script>175 </head>176 <body>177 <a href='http://github.com/visionmedia/express'> 178 <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> 179 </a>180 <div id="wrapper">181 <div id="container">182 <a href='http://github.com/visionmedia/express' id='logo'>Express</a> 183 <p id="tagline">184 High performance, high class web development for185 <a href="http://nodejs.org">Node.js</a>186 </p>187 <ul id="menu">188 <li><a href="index.html">Home</a></li>189 <li><a href="guide.html">Guide</a></li>190 <li><a href="screencasts.html">Screencasts</a></li>191 <li><a href="applications.html">Applications</a></li>192 </ul>

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

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

Fixed two testsTj Holowaychukbefore adoptiondf49769

line 194why, one step back
194

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

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

docsTj Holowaychukbefore adoptionc1a82bf

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

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

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

docsTj Holowaychukbefore adoptionc1a82bf

line 206why, one step back
206

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

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

docsTj Holowaychukbefore adoptionc1a82bf

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

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

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

docsTj Holowaychukbefore adoptionc1a82bf

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

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

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

docsTj Holowaychukbefore adoptionc1a82bf

line 219why, one step back
219

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

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

docsTj Holowaychukbefore adoptionc1a82bf

lines 221–234why, one step back
221
222<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>223224<p> For example we may utilize this feature to create locals in middleware:</p>225226<pre><code> function loadUser(req, res, next) {227 User.get(req.params.id, function(err, user){228 res.local('user', user);229 next();230 });231 }232233 app.get('/user/:id', loadUser, function(req, res){234 res.render('user');

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

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

docsTj Holowaychukbefore adoptionc1a82bf

line 237why, one step back
237

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

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

docsTj Holowaychukbefore adoption1db2efa

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

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

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

docsTj Holowaychukbefore adoption1db2efa

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

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

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

docsTj Holowaychukbefore adoption1db2efa

line 249why, one step back
249

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

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

docsTj Holowaychukbefore adoption1db2efa

lines 251–253why, one step back
251
252<p> <em>app.locals()</em> is now an alias of <em>app.helpers()</em>, as helpers makes more sense for functions.</p>253

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

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

docsTj Holowaychukbefore adoption1db2efa

lines 255–271why, one step back
255
256<p> <em>req.accepts()</em> now accepts extensions:</p>257258<pre><code> // Accept: text/html259 req.accepts('html');260 req.accepts('.html');261 // =&gt; true262263 // Accept: text/*; application/json264 req.accepts('html');265 req.accepts('text/*');266 req.accepts('text/plain');267 req.accepts('application/json');268 // =&gt; true269270 req.accepts('image/png');271 req.accepts('png');

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

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

docsTj Holowaychukbefore adoption1db2efa

line 274why, one step back
274

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

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

docsTj Holowaychukbefore adoption131970b

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

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

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

docsTj Holowaychukbefore adoption131970b

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

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

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

docsTj Holowaychukbefore adoption131970b

line 286why, one step back
286

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

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

docsTj Holowaychukbefore adoption1db2efa

line 288why, one step back
288

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

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

docsTj Holowaychukbefore adoption1db2efa

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

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

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

docsTj Holowaychukbefore adoption1db2efa

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

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

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

docsTj Holowaychukbefore adoption2bb0412

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

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

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

docsTj Holowaychukbefore adoption2bb0412

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

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

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

docsTj Holowaychukbefore adoption2bb0412

line 315why, one step back
315

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

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

docsTj Holowaychukbefore adoption2bb0412

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

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

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

docsTj Holowaychukbefore adoption2bb0412

line 326why, one step back
326

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

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

lines 328–330why, one step back
328
329<p> Both <em>res.partial()</em> and the <em>partial()</em> functions accept an 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>330

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

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

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

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

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

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

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

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 348why, one step back
348

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

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

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

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 354why, one step back
354

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

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

docsTj Holowaychukbefore adoption1db2efa

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

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

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

docsTj Holowaychukbefore adoption1db2efa

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

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

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

docsTj Holowaychukbefore adoption1db2efa

line 367why, one step back
367

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

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

docsTj Holowaychukbefore adoption1db2efa

line 369why, one step back
369

docsTj Holowaychukbefore adoption5581ca2

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

docsTj Holowaychukbefore adoption1db2efa

line 371why, one step back
371

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

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

docsTj Holowaychukbefore adoption1db2efa

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

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

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

docsTj Holowaychukbefore adoption1db2efa

line 378why, one step back
378

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

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

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

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

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

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

Fixed two testsTj Holowaychukbefore adoptiondf49769

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