Skip to content
Myr

migrate.html

This file

Path

docs/migrate.html

412 lines · 10.9 KiB · HTML

Last changed by

Finished app page (for now)Tj Holowaychukbefore adoption7cbb737

History of this file

Take it

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: 65px;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="contrib.html">Contributing</a></li>191					<li><a href="applications.html">Applications</a></li>192				</ul>193<div class='mp'>194<h2 id="Express">Express</h2>195<p class="man-name">196  <code>migrate</code>197</p>198<h3 id="Built-On-Connect">Built On Connect</h3>199200<p>Express 1.x is written to run on-top of the <a href="http://extjs.github.com/Connect">Connect</a> middlware201framework, thus the <em>Plugin</em> has been replaced by Connect's middleware. By abstracting our middleware202to Connect we allow additional community frameworks to develop robust, high-level frameworks using203the same technologies as Express.</p>204205<h3 id="Creating-Applications">Creating Applications</h3>206207<p>Previously due to legacy code implemented in the early days of node,208Express unfortunately had some globals. The DSL would previously be209accessed as shown below:</p>210211<pre><code>require('express');212213configure(function(){214    // app configuration215});216217get('/', function(){218    return 'hello world';219});220</code></pre>221222<p>Now we utilize the CommonJS module system appropriately, and223introduce <em>express.createServer()</em> which accepts the same arguments224as <em>http.createServer()</em>:</p>225226<pre><code>var express = require('express'),227    app = express.createServer();228229app.configure(function(){230    // app configuration231});232233app.get('/', function(req, res){234    res.send('hello world');235});236</code></pre>237238<p>Express 1.x does <em>not</em> currently allow returning of a string.</p>239240<h3 id="Plugins-vs-Middleware">Plugins vs Middleware</h3>241242<p>Previously Express was bundled with plugins, which were essentially what243are now Connect middleware. Previously plugins would be utilized in a manor244similar to below:</p>245246<pre><code>use(Logger);247use(MethodOverride);248use(Cookie);249</code></pre>250251<p>Which we can now <em>use()</em> within our app, or pass to the <em>express.createServer()</em> method:</p>252253<pre><code>var app = express.createServer(254    express.logger(),255    express.methodOverride(),256    express.cookieDecoder()257);258</code></pre>259260<p>or:</p>261262<pre><code>var app = express.createServer();263264app.use(express.logger());265app.use(express.methodOverride());266app.use(express.cookieDecoder());267</code></pre>268269<p>For documentation on creating Connect middleware visit <a href="http://extjs.github.com/Connect/#Middleware-Authoring">Middleware Authoring</a>.</p>270271<h3 id="Running-Applications">Running Applications</h3>272273<p>Previously a global function <em>run()</em>, was available:</p>274275<pre><code>run();276</code></pre>277278<p>The new <em>express.Server</em> has the same API as <em>http.Server</em>,279so we can do things like:</p>280281<pre><code>app.listen();282app.listen(3000);283</code></pre>284285<h3 id="Route-Parameters">Route Parameters</h3>286287<p>Previously we could use <em>this.param()</em> to attempt288fetching a route, query string, or request body parameter:</p>289290<pre><code>get('/user/:id', function(){291    this.param('id');292});293</code></pre>294295<p>Polymorphic parameter access can be done using <code>req.param()</code>:</p>296297<pre><code>app.get('/user/:id', function(req, res){298    req.param('id');299});300</code></pre>301302<p>Route parameters are available via <code>req.params</code>:</p>303304<pre><code>app.get('/user/:id', function(req, res){305    req.params.id;306});307</code></pre>308309<h3 id="Passing-Route-Control">Passing Route Control</h3>310311<p>Old express had a weak notion of route passing,312which did not support async, and was never properly313implemented for practical use:</p>314315<pre><code>get('/', function(){316    this.pass('/foobar');317});318</code></pre>319320<p>Now Express has access to Connect's <em>next()</em> function,321which is passed as the third and final argument. Calling <em>next()</em> will322pass control to the next <em>matching route</em>, or continue down the stack323of Connect middleware.</p>324325<pre><code>app.get('/user/:id?', function(req, res, next){326    next();327});328329app.get('/user', function(){330    // ... respond331});332</code></pre>333334<h3 id="View-Rendering">View Rendering</h3>335336<p>View filenames no longer take the form <em>Express</em>.<em>TYPE</em>.<em>ENGINE</em>,337the <em>Content-Type</em> can be set via <em>res.contentType()</em> or338<em>res.header()</em>. For example what was previously <em>layout.html.haml</em>,339should now be <em>layout.haml</em>.</p>340341<p>Previously a view render looked something like this:</p>342343<pre><code>get('/', function(){344    this.render('index.html.haml', {345        locals: { title: 'My Site' }346    });347});348</code></pre>349350<p>We now have <em>res.render()</em>, however the options passed to <a href="http://github.com/visionmedia/haml.js">haml</a>, <a href="http://github.com/visionmedia/jade">jade</a>, and others351remain the same.</p>352353<pre><code>app.get('/', function(req, res){354    res.render('index.haml', {355        locals: { title: 'My Site' }356    });357});358</code></pre>359360<p>Previously rendering of a collection via <em>partial()</em> would look something like this:</p>361362<pre><code>this.partial('comment.html.haml', { collection: comments });363</code></pre>364365<p>Although this worked just fine, it was generally to verbose, the similar but new API366looks like this, as <em>partial()</em> is <em>always</em> passed as a local variable:</p>367368<pre><code>partial('comment.haml', { collection: comments });369</code></pre>370371<p>To make things even less verbose we can assume the extension when omitted:</p>372373<pre><code>partial('comment', { collection: comments });374</code></pre>375376<p>And once again even further, when rendering a collection we can simply pass377an array, if no other options are desired:</p>378379<pre><code>partial('comments', comments);380</code></pre>381382<h3 id="Redirecting">Redirecting</h3>383384<p>Previously you would</p>385386<pre><code>this.redirect('/somewhere');387</code></pre>388389<p>However you would now:</p>390391<pre><code>res.redirect('/somewhere');392res.redirect('/somewhere', 301);393</code></pre>394395<h3 id="HTTP-Client">HTTP Client</h3>396397<p>Previously Express provided a high level http client, this library is no more398as it does not belong in Express, however it may be resurrected as a separate module.</p>399400<h3 id="Core-Extensions">Core Extensions</h3>401402<p>Express is no longer dependent on the <a href="http://github.com/visionmedia/ext.js">JavaScript Extensions</a> library, so those of you using the methods provided by it such as <code>Object.merge(a, b)</code> will need to403roll your own, or install the module via:</p>404405<pre><code>$ npm install ext406</code></pre>407408</div>409			</div>410		</div>411	</body>412</html>