migrate.html
This file
Path
docs/migrate.html
398 lines · 10.8 KiB · HTML
Last changed by
More view docsTj Holowaychukbefore adoption1c8ff48
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399<html> <head> <title>Express - node web framework</title> <style> #header { position: absolute; top: 10px; left: 0; padding: 12px 0; text-indent: 40px; width: 100%; border-top: 1px solid rgba(0,0,0,0.7); border-bottom: 1px solid rgba(0,0,0,0.7); background: rgba(255,255,255,0.1) url(http://www.sencha.com/favicon.ico) no-repeat 15px 50%; text-align: left; color: #fff; } #tagline { margin-left: 75px; margin-bottom: 30px; color: rgba(255,255,255,0.7); } html { background: #1c1c1c url(images/bg.tile.jpg); }
body { margin: 0; padding-bottom: 30px; font: 14px/1.4 "Helvetica Neue", "Lucida Grande", "Arial"; font-size: 14px; line-height: 1.5; -webkit-text-stroke: 1px rgba(0, 0, 0, 0.1); -moz-text-stroke: 1px rgba(0, 0, 0, 0.1); background: url(images/bg.jpg) 50% 0 no-repeat; color: #8b8b8b; }
* { outline: none; }
em { color: white; }
a img { border: none !important; }
a { font-weight: bold; text-decoration: none; color: white; -webkit-transition-property: opacity, -webkit-transform, color, background-color, padding, -webkit-box-shadow; -webkit-transition-duration: 0.15s; -webkit-transition-timing-function: ease-out; } a:hover { opacity: 0.8; }
h1, h2, h3 { margin: 45px 0 0 0; color: white; text-shadow: 1px 2px 2px rgba(0,0,0,0.6); }
h3 { font-size: 18px; }
pre { margin: 20px 10px; padding: 25px 20px; background: rgba(0,0,0,0.5); border: 1px solid #323232; -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -webkit-border-radius: 5px; -moz-border-radius: 5px; }
code { font-family: "Helvetica Neue", "Lucida Grande", "Arial"; }
ul { margin: 15px 0; padding: 0 0 0 35px; } ul li { margin: 0; padding: 2px 0; list-style: square; }
.sect { margin-left: 40px; }
#logo { display: block; margin-left: 30%; margin-bottom: 30px; width: 194px; height: 51px; background: url(images/logo.png) 0 0 no-repeat; text-indent: -99999px; } #logo:hover { opacity: 0.7; } #logo:active { opacity: 0.3; }
#ribbon { position: fixed; top: 0; right: 0; z-index: 2; }
#wrapper { width: 100%; min-height: 800px; background: url(images/top.png) 0 0 repeat-x; }
#container { margin: 0 auto; padding-top: 110px; width: 550px; }
#toc { position: fixed; top: 60px; left: 0; margin: 0 0 0 15px; padding: 15px; height: 100%; background: rgba(0,0,0,0.2); border-right: 1px solid rgba(255,255,255,0.05); } #toc li { padding: 0; list-style: none; } #toc li a { font-size: 11px; } #menu { margin-left: 65px; padding: 0; padding-bottom: 30px; } #menu li { display: inline; list-style: none; } #menu li a { display: block; float: left; margin: 0 2px; padding: 3px 15px; background: rgba(0,0,0,0.2); -webkit-border-radius: 8px; -moz-border-radius: 8px; -webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -moz-box-shadow: 1px 2px 2px rgba(0,0,0,0.6); -webkit-transition-property: opacity, -webkit-transform, color, background-color, -webkit-box-shadow; -webkit-transition-duration: 0.15s; -webkit-transition-timing-function: ease-out; } #menu li a:hover, #menu li a.active { background: rgba(0,0,0,0.5); } #menu li a:active { background: rgba(0,0,0,0.1); -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.4); } </style> </head> <body> <a href='http://github.com/visionmedia/express'> <img alt='Fork me on GitHub' id='ribbon' src='http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png' /> </a> <div id="header"><strong>Sencha</strong> labs</div> <div id="wrapper"> <div id="container"> <a href='http://github.com/visionmedia/express' id='logo'>Express</a> <p id="tagline"> High performance, high class web development for <a href="http://nodejs.org">Node.js</a> </p> <ul id="menu"> <li><a href="index.html">Home</a></li> <li><a href="guide.html">Guide</a></li> <li><a href="contrib.html">Contributing</a></li> <li><a href="migrate.html">1.x Migration</a></li> </ul><div class='mp'><h3 id="Built-On-Connect">Built On Connect</h3>
<p>Express 1.x is written to run on-top of the <a href="http://extjs.github.com/Connect">Connect</a> middlwareframework, thus the <em>Plugin</em> has been replaced by Connect's middleware. By abstracting our middlewareto Connect we allow additional community frameworks to develop robust, high-level frameworks usingthe same technologies as Express.</p>
<h3 id="Creating-Applications">Creating Applications</h3>
<p>Previously due to legacy code implemented in the early days of node,Express unfortunately had some globals. The DSL would previously beaccessed as shown below:</p>
<pre><code>require('express');
configure(function(){ // app configuration});
get('/', function(){ return 'hello world';});</code></pre>
<p>Now we utilize the CommonJS module system appropriately, andintroduce <em>express.createServer()</em> which accepts the same argumentsas <em>http.createServer()</em>:</p>
<pre><code>var express = require('express'), app = express.createServer();
app.configure(function(){ // app configuration});
app.get('/', function(req, res){ res.send('hello world');});</code></pre>
<p>Express 1.x does <em>not</em> currently allow returning of a string.</p>
<h3 id="Plugins-vs-Middleware">Plugins vs Middleware</h3>
<p>Previously Express was bundled with plugins, which were essentially whatare now Connect middleware. Previously plugins would be utilized in a manorsimilar to below:</p>
<pre><code>use(Logger);use(MethodOverride);use(Cookie);</code></pre>
<p>Which we can now <em>use()</em> within our app, or pass to the <em>express.createServer()</em> method:</p>
<pre><code>var connect = require('connect');
var app = express.createServer( connect.logger(), connect.methodOverride(), connect.cookie());</code></pre>
<p>or:</p>
<pre><code>var connect = require('connect');var app = express.createServer();
app.use('/', connect.logger());app.use('/', connect.methodOverride());app.use('/', connect.cookieDecoder());</code></pre>
<p>For documentation on creating Connect middleware visit <a href="http://extjs.github.com/Connect/#Middleware-Authoring">Middleware Authoring</a>.</p>
<h3 id="Running-Applications">Running Applications</h3>
<p>Previously a global function <em>run()</em>, was available:</p>
<pre><code>run();</code></pre>
<p>The new <em>express.Server</em> has the same API as <em>http.Server</em>,so we can do things like:</p>
<pre><code>app.listen();app.listen(3000);</code></pre>
<h3 id="Route-Parameters">Route Parameters</h3>
<p>Previously we could use <em>this.param()</em> to attemptfetching a route, query string, or request body parameter:</p>
<pre><code>get('/user/:id', function(){ this.param('id');});</code></pre>
<p>Polymorphic parameter access can be done using <code>req.param()</code>:</p>
<pre><code>app.get('/user/:id', function(){ req.param('id');});</code></pre>
<p>Route parameters are also passed as the third argument:</p>
<pre><code>app.get('/user/:id', function(req, res, params){ params.id;});</code></pre>
<h3 id="Passing-Route-Control">Passing Route Control</h3>
<p>Old express had a weak notion of route passing,which did not support async, and was never properlyimplemented for practical use:</p>
<pre><code>get('/', function(){ this.pass('/foobar');});</code></pre>
<p>Now Express has access to Connect's <em>next()</em> function,which is passed as the fourth and final argument. Calling <em>next()</em> willpass control to the next <em>matching route</em>, or continue down the stackof Connect middleware.</p>
<pre><code>app.get('/user/:id?', function(req, res, params, next){ next();});
app.get('/user', function(){ // ... respond});</code></pre>
<h3 id="View-Rendering">View Rendering</h3>
<p>View filenames no longer take the form <em>Express</em>.<em>TYPE</em>.<em>ENGINE</em>,the <em>Content-Type</em> can be set via <em>res.contentType()</em> or<em>res.header()</em>. For example what was previously <em>layout.html.haml</em>,should now be <em>layout.haml</em>.</p>
<p>Previously a view render looked something like this:</p>
<pre><code>get('/', function(){ this.render('index.html.haml', { locals: { title: 'My Site' } });});</code></pre>
<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 othersremain the same.</p>
<pre><code>app.get('/', function(req, res){ res.render('index.haml', { locals: { title: 'My Site' } });});</code></pre>
<p>Previously rendering of a collection via <em>partial()</em> would look something like this:</p>
<pre><code>this.partial('comment.html.haml', { collection: comments });</code></pre>
<p>Although this worked just fine, it was generally to verbose, the similar but new APIlooks like this, as <em>partial()</em> is <em>always</em> passed as a local variable:</p>
<pre><code>partial('comment.haml', { collection: comments });</code></pre>
<p>To make things even less verbose we can assume the extension when omitted:</p>
<pre><code>partial('comment', { collection: comments });</code></pre>
<p>And once again even further, when rendering a collection we can simply passan array, if no other options are desired:</p>
<pre><code>partial('comments', comments);</code></pre>
<h3 id="Redirecting">Redirecting</h3>
<p>Previously you would</p>
<pre><code>this.redirect('/somewhere');</code></pre>
<p>However you would now:</p>
<pre><code>res.redirect('/somewhere');res.redirect('/somewhere', 301);</code></pre>
<h3 id="HTTP-Client">HTTP Client</h3>
<p>Previously Express provided a high level http client, this library is no moreas it does not belong in Express, however it may be resurrected as a separate module.</p>
<h3 id="Core-Extensions">Core Extensions</h3>
<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 toroll your own, or install the module via:</p>
<pre><code>$ npm install ext</code></pre>
</div> </div> </div> </body></html>