Skip to content
Myr

migrate.md

This file

Path

docs/migrate.md

192 lines · 5.0 KiB · Markdown

Last changed by

Updated docsTj Holowaychukbefore adoptiond691ecb

History of this file

Take it

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

Added app.redirect(key, url)Tj Holowaychukbefore adoptionb75246e

lines 1–58why, one step back
1
2### Built On Connect34Express 1.x is written to run on-top of the [Connect](http://extjs.github.com/Connect) middlware5framework, thus the _Plugin_ has been replaced by Connect's middleware. By abstracting our middleware6to Connect we allow additional community frameworks to develop robust, high-level frameworks using7the same technologies as Express.89### Creating Applications1011Previously due to legacy code implemented in the early days of node,12Express unfortunately had some globals. The DSL would previously be13accessed as shown below:1415 require('express');1617 configure(function(){18 // app configuration19 });2021 get('/', function(){22 return 'hello world';23 });2425Now we utilize the CommonJS module system appropriately, and26introduce _express.createServer()_ which accepts the same arguments27as _http.createServer()_:2829 var express = require('express'),30 app = express.createServer();31 32 app.configure(function(){33 // app configuration34 });35 36 app.get('/', function(req, res){37 res.send('hello world');38 });3940Express 1.x does _not_ currently allow returning of a string.4142### Plugins vs Middleware4344Previously Express was bundled with plugins, which were essentially what45are now Connect middleware. Previously plugins would be utilized in a manor46similar to below:4748 use(Logger);49 use(MethodOverride);50 use(Cookie);5152Which we can now _use()_ within our app, or pass to the _express.createServer()_ method:5354 var connect = require('connect');5556 var app = express.createServer(57 connect.logger(),58 connect.methodOverride(),

Fixed typo in docsTj Holowaychukbefore adoption0a84824

line 59why, one step back
59 connect.cookieDecoder()

Added app.redirect(key, url)Tj Holowaychukbefore adoptionb75246e

lines 60–66why, one step back
60 );
6162or:6364 var connect = require('connect');65 var app = express.createServer();66

Updated docsTj Holowaychukbefore adoptioncacada8

lines 67–69why, one step back
67 app.use(connect.logger());
68 app.use(connect.methodOverride());69 app.use(connect.cookieDecoder());

Added app.redirect(key, url)Tj Holowaychukbefore adoptionb75246e

lines 70–95why, one step back
70
71For documentation on creating Connect middleware visit [Middleware Authoring](http://extjs.github.com/Connect/#Middleware-Authoring).7273### Running Applications7475Previously a global function _run()_, was available:7677 run();7879The new _express.Server_ has the same API as _http.Server_,80so we can do things like:8182 app.listen();83 app.listen(3000);8485### Route Parameters8687Previously we could use _this.param()_ to attempt88fetching a route, query string, or request body parameter:8990 get('/user/:id', function(){91 this.param('id');92 });9394Polymorphic parameter access can be done using `req.param()`:95

Fixed migration typoTj Holowaychukbefore adoptiona9c3f56

line 96why, one step back
96 app.get('/user/:id', function(req, res){

Added app.redirect(key, url)Tj Holowaychukbefore adoptionb75246e

lines 97–99why, one step back
97 req.param('id');
98 });99

Updated docsTj Holowaychukbefore adoptiond691ecb

line 100why, one step back
100Route parameters are available via `req.params`:

Added app.redirect(key, url)Tj Holowaychukbefore adoptionb75246e

line 101why, one step back
101

Updated docsTj Holowaychukbefore adoptiond691ecb

lines 102–103why, one step back
102 app.get('/user/:id', function(req, res){
103 req.params.id;

Added app.redirect(key, url)Tj Holowaychukbefore adoptionb75246e

lines 104–116why, one step back
104 });
105106### Passing Route Control107108Old express had a weak notion of route passing,109which did not support async, and was never properly 110implemented for practical use:111112 get('/', function(){113 this.pass('/foobar');114 });115116Now Express has access to Connect's _next()_ function,

Updated docsTj Holowaychukbefore adoptiond691ecb

line 117why, one step back
117which is passed as the third and final argument. Calling _next()_ will

Added app.redirect(key, url)Tj Holowaychukbefore adoptionb75246e

lines 118–120why, one step back
118pass control to the next _matching route_, or continue down the stack
119of Connect middleware.120

Updated docsTj Holowaychukbefore adoptiond691ecb

line 121why, one step back
121 app.get('/user/:id?', function(req, res, next){

Added app.redirect(key, url)Tj Holowaychukbefore adoptionb75246e

lines 122–161why, one step back
122 next();
123 });124125 app.get('/user', function(){126 // ... respond127 });128129### View Rendering130131View filenames no longer take the form _NAME_._TYPE_._ENGINE_,132the _Content-Type_ can be set via _res.contentType()_ or133_res.header()_. For example what was previously _layout.html.haml_,134should now be _layout.haml_.135136Previously a view render looked something like this:137138 get('/', function(){139 this.render('index.html.haml', {140 locals: { title: 'My Site' }141 });142 });143144We now have _res.render()_, however the options passed to [haml](http://github.com/visionmedia/haml.js), [jade](http://github.com/visionmedia/jade), and others145remain the same.146147 app.get('/', function(req, res){148 res.render('index.haml', {149 locals: { title: 'My Site' }150 });151 });152153Previously rendering of a collection via _partial()_ would look something like this:154155 this.partial('comment.html.haml', { collection: comments });156157Although this worked just fine, it was generally to verbose, the similar but new API158looks like this, as _partial()_ is _always_ passed as a local variable:159160 partial('comment.haml', { collection: comments });161

Added more view docsTj Holowaychukbefore adoption2f96506

line 162why, one step back
162To make things even less verbose we can assume the extension when omitted:

Added app.redirect(key, url)Tj Holowaychukbefore adoptionb75246e

lines 163–192why, one step back
163
164 partial('comment', { collection: comments });165166And once again even further, when rendering a collection we can simply pass167an array, if no other options are desired:168169 partial('comments', comments);170171### Redirecting172173Previously you would174175 this.redirect('/somewhere');176177However you would now:178179 res.redirect('/somewhere');180 res.redirect('/somewhere', 301);181182### HTTP Client183184Previously Express provided a high level http client, this library is no more185as it does not belong in Express, however it may be resurrected as a separate module.186187### Core Extensions188189Express is no longer dependent on the [JavaScript Extensions](http://github.com/visionmedia/ext.js) library, so those of you using the methods provided by it such as `Object.merge(a, b)` will need to190roll your own, or install the module via:191192 $ npm install ext