Skip to content
Myr

migrate.html

This file

Path

docs/migrate.html

371 lines · 12.1 KiB · HTML

Last changed by

regenerated docsTj Holowaychukbefore adoptiondd7158a

History of this file

Take it

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

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>

docsTj Holowaychukbefore adoption4609984

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

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

line 194why, one step back
194

docsTj Holowaychukbefore adoptionc1a82bf

lines 195–203why, one step back
195<h3 id="HTTPS">HTTPS</h3>
196197<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

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

docsTj Holowaychukbefore adoptionc1a82bf

lines 205–210why, one step back
205
206<h3 id="req-header-Referrer">req.header() Referrer</h3>207208<p> Previously if anyone was doing something similar to:</p>209210<pre><code> req.headers.referrer || req.headers.referer

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 211why, one step back
211 req.header('Referrer') || req.header('Referer')</code></pre>

docsTj Holowaychukbefore adoptionc1a82bf

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 215why, one step back
215<pre><code> req.header('Referrer')</code></pre>

docsTj Holowaychukbefore adoptionc1a82bf

line 216why, one step back
216

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 217why, one step back
217<h3 id="res-local-name-val">res.local(name, val)</h3>

docsTj Holowaychukbefore adoptionc1a82bf

lines 218–231why, one step back
218
219<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>220221<p> For example we may utilize this feature to create locals in middleware:</p>222223<pre><code> function loadUser(req, res, next) {224 User.get(req.params.id, function(err, user){225 res.local('user', user);226 next();227 });228 }229230 app.get('/user/:id', loadUser, function(req, res){231 res.render('user');

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 232why, one step back
232 });</code></pre>

docsTj Holowaychukbefore adoptionc1a82bf

line 233why, one step back
233

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 234why, one step back
234<h3 id="req-param-name-defaultValue">req.param(name[, defaultValue])</h3>

docsTj Holowaychukbefore adoption1db2efa

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 238why, one step back
238<pre><code> var id = req.param('id') || req.user.id;</code></pre>

docsTj Holowaychukbefore adoption1db2efa

lines 239–241why, one step back
239
240<p> The new <em>defaultValue</em> argument can handle this nicely:</p>241

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

docsTj Holowaychukbefore adoption1db2efa

line 243why, one step back
243

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 244why, one step back
244<h3 id="app-helpers-app-locals">app.helpers() / app.locals()</h3>

docsTj Holowaychukbefore adoption1db2efa

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 248why, one step back
248<h3 id="req-accepts-type">req.accepts(type)</h3>

docsTj Holowaychukbefore adoption1db2efa

lines 249–265why, one step back
249
250<p> <em>req.accepts()</em> now accepts extensions:</p>251252<pre><code> // Accept: text/html253 req.accepts('html');254 req.accepts('.html');255 // =&gt; true256257 // Accept: text/*; application/json258 req.accepts('html');259 req.accepts('text/*');260 req.accepts('text/plain');261 req.accepts('application/json');262 // =&gt; true263264 req.accepts('image/png');265 req.accepts('png');

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 266why, one step back
266 // =&gt; false</code></pre>

docsTj Holowaychukbefore adoption1db2efa

line 267why, one step back
267

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 268why, one step back
268<h3 id="res-cookie">res.cookie()</h3>

docsTj Holowaychukbefore adoption131970b

lines 269–271why, one step back
269
270<p> Previously only directly values could be passed, so for example:</p>271

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

docsTj Holowaychukbefore adoption131970b

lines 273–275why, one step back
273
274<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>275

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

docsTj Holowaychukbefore adoption131970b

line 277why, one step back
277

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 278why, one step back
278<h3 id="res-download-res-sendfile">res.download() / res.sendfile()</h3>

docsTj Holowaychukbefore adoption1db2efa

lines 279–289why, one step back
279
280<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>281282<pre><code> res.download('/path/to/file');283284 res.download('/path/to/file', function(err){285 if (err) {286 console.error(err);287 } else {288 console.log('transferred');289 }

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 290why, one step back
290 });</code></pre>

docsTj Holowaychukbefore adoption1db2efa

lines 291–293why, one step back
291
292<p> The <em>stream threshold</em> setting was removed.</p>293

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 294why, one step back
294<h3 id="res-render">res.render()</h3>

docsTj Holowaychukbefore adoption2bb0412

lines 295–297why, one step back
295
296<p> Previously locals were passed as a separate key:</p>297

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

docsTj Holowaychukbefore adoption2bb0412

lines 299–301why, one step back
299
300<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>301

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 302why, one step back
302<pre><code> res.render('user', { layout: false, user: user });</code></pre>

docsTj Holowaychukbefore adoption2bb0412

line 303why, one step back
303

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 304why, one step back
304<h3 id="res-partial">res.partial()</h3>

docsTj Holowaychukbefore adoption2bb0412

lines 305–311why, one step back
305
306<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>307308<pre><code> // render a collection of comments309 res.partial('comment', [comment1, comment2]); 310311 // render a single comment

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 312why, one step back
312 res.partial('comment', comment);</code></pre>

docsTj Holowaychukbefore adoption2bb0412

line 313why, one step back
313

regenerated docsTj Holowaychukbefore adoptiondd7158a

lines 314–336why, one step back
314<h3 id="partial-locals">partial() locals</h3>
315316<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>317318<pre><code> partial('user', { object: user, locals: { date: new Date }})</code></pre>319320<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>321322<pre><code> partial('user', { locals: { user: user, date: new Date }})</code></pre>323324<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>325326<pre><code> partial('user', { object: user, date: new Date })</code></pre>327328<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>329330<pre><code> partial('user', { user: user, date: new Date })</code></pre>331332<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>333334<pre><code> partial('user', user); 335 partial('user', { object: user }); </code></pre>336

docsTj Holowaychukbefore adoption2bb0412

line 337why, one step back
337<h3 id="Template-Engine-Compliance">Template Engine Compliance</h3>

docsTj Holowaychukbefore adoption1db2efa

lines 338–340why, one step back
338
339<p> To comply with Express previously engines needed the following signature:</p>340

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 341why, one step back
341<pre><code> engine.render(str, options, function(err){});</code></pre>

docsTj Holowaychukbefore adoption1db2efa

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

regenerated docsTj Holowaychukbefore adoptiondd7158a

line 346why, one step back
346 fn(locals);</code></pre>

docsTj Holowaychukbefore adoption1db2efa

lines 347–349why, one step back
347
348<h3 id="View-Partial-Lookup">View Partial Lookup</h3>349

docsTj Holowaychukbefore adoption5581ca2

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

lines 351–355why, one step back
351
352<h3 id="Mime-Types">Mime Types</h3>353354<p> Express and Connect now utilize the <em>mime</em> module in npm, so to add more use:</p>355

regenerated docsTj Holowaychukbefore adoptiondd7158a

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

docsTj Holowaychukbefore adoption1db2efa

line 357why, one step back
357

regenerated docsTj Holowaychukbefore adoptiondd7158a

lines 358–367why, one step back
358<h3 id="static-middleware">static() middleware</h3>
359360<p> Previously named <code>staticProvider()</code>, the now <code>static()</code> middleware takes a single directory path, followed by options.</p>361362<pre><code> app.use(express.static(__dirname + '/public', { maxAge: oneYear }));</code></pre>363364<p>Previously when using options the <code>root</code> option would be used for this:</p>365366<pre><code> app.use(express.staticProvider({ root: __dirname + '/public', maxAge: oneYear }));367 </code></pre>

moved files from lib/express/* to lib/Tj Holowaychukbefore adoption3d597a5

lines 368–371why, one step back
368 </div>
369 </div>370 </body>371</html>