migrate.html
This file
Path
docs/migrate.html
394 lines · 11.8 KiB · HTML
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 05b76a1 point at it.
prepping blog example for new expressTj Holowaychukbefore adoption05b76a1
lines 1–394why, one step back1<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); }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>194<h3>Express 1.x to 2.x Migration</h3>195196<h3>HTTPS</h3>197198<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 });204205 app.listen(443);206</code></pre>207208<h3>req.header() Referrer</h3>209210<p> Previously if anyone was doing something similar to:</p>211212<pre><code> req.headers.referrer || req.headers.referer213 req.header('Referrer') || req.header('Referer')214</code></pre>215216<p> With the new special-case we may now simply use <em>Referrer</em> which will return either if defined:</p>217218<pre><code> req.header('Referrer')219</code></pre>220221<h3>res.local(name, val)</h3>222223<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');236 });237</code></pre>238239<h3>req.param(name[, defaultValue])</h3>240241<p> Previously only <em>name</em> was accepted, so some of you may have been doing the following:</p>242243<pre><code> var id = req.param('id') || req.user.id;244</code></pre>245246<p> The new <em>defaultValue</em> argument can handle this nicely:</p>247248<pre><code> var id = req.param('id', req.user.id);249</code></pre>250251<h3>app.helpers() / app.locals()</h3>252253<p> <em>app.locals()</em> is now an alias of <em>app.helpers()</em>, as helpers makes more sense for functions.</p>254255<h3>req.accepts(type)</h3>256257<p> <em>req.accepts()</em> now accepts extensions:</p>258259<pre><code> // Accept: text/html260 req.accepts('html');261 req.accepts('.html');262 // => true263264 // Accept: text/*; application/json265 req.accepts('html');266 req.accepts('text/*');267 req.accepts('text/plain');268 req.accepts('application/json');269 // => true270271 req.accepts('image/png');272 req.accepts('png');273 // => false274</code></pre>275276<h3>res.cookie()</h3>277278<p> Previously only directly values could be passed, so for example:</p>279280<pre><code>res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000) });281</code></pre>282283<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>284285<pre><code>res.cookie('rememberme', 'yes', { maxAge: 900000 });286</code></pre>287288<h3>res.download() / res.sendfile()</h3>289290<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>291292<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 }300 });301</code></pre>302303<p> The <em>stream threshold</em> setting was removed.</p>304305<h3>res.render()</h3>306307<p> Previously locals were passed as a separate key:</p>308309<pre><code> res.render('user', { layout: false, locals: { user: user }});310</code></pre>311312<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>313314<pre><code> res.render('user', { layout: false, user: user });315</code></pre>316317<h3>res.partial()</h3>318319<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 comment325 res.partial('comment', comment);326</code></pre>327328<h3>partial() locals</h3>329330<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>331332<pre><code> partial('user', { object: user, locals: { date: new Date }})333</code></pre>334335<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>336337<pre><code> partial('user', { locals: { user: user, date: new Date }})338</code></pre>339340<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>341342<pre><code> partial('user', { object: user, date: new Date })343</code></pre>344345<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>346347<pre><code> partial('user', { user: user, date: new Date })348</code></pre>349350<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>351352<pre><code> partial('user', user); 353 partial('user', { object: user }); 354</code></pre>355356<h3>Template Engine Compliance</h3>357358<p> To comply with Express previously engines needed the following signature:</p>359360<pre><code> engine.render(str, options, function(err){});361</code></pre>362363<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);366 fn(locals);367</code></pre>368369<h3>View Partial Lookup</h3>370371<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>372373<h3>Mime Types</h3>374375<p> Express and Connect now utilize the <em>mime</em> module in npm, so to add more use:</p>376377<pre><code> require('mime').define({ 'foo/bar': ['foo', 'bar'] });378</code></pre>379380<h3>static() middleware</h3>381382<p> Previously named <code>staticProvider()</code>, the now <code>static()</code> middleware takes a single directory path, followed by options.</p>383384<pre><code> app.use(express.static(__dirname + '/public', { maxAge: oneYear }));385</code></pre>386387<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 }));390</code></pre>391 </div>392 </div>393 </body>394</html>