migrate.md
This file
Path
docs/migrate.md
189 lines · 4.9 KiB · Markdown
Last changed by
Updated docsTj Holowaychukbefore adoption344218f
History of this fileTake it
The walk stopped after 200 commits: lines older than 5e1a271 point at it.
Fixed res.render() callback when a layout is used [thanks guillermo]Tj Holowaychukbefore adoption5e1a271
lines 1–54why, one step back12### 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 app = express.createServer(Updated docsTj Holowaychukbefore adoption344218f
lines 55–57why, one step back55 express.logger(),56 express.methodOverride(),57 express.cookieDecoder()Fixed res.render() callback when a layout is used [thanks guillermo]Tj Holowaychukbefore adoption5e1a271
lines 58–63why, one step back58 );5960or:6162 var app = express.createServer();63Updated docsTj Holowaychukbefore adoption344218f
lines 64–66why, one step back64 app.use(express.logger());65 app.use(express.methodOverride());66 app.use(express.cookieDecoder());Fixed res.render() callback when a layout is used [thanks guillermo]Tj Holowaychukbefore adoption5e1a271
lines 67–189why, one step back6768For documentation on creating Connect middleware visit [Middleware Authoring](http://extjs.github.com/Connect/#Middleware-Authoring).6970### Running Applications7172Previously a global function _run()_, was available:7374 run();7576The new _express.Server_ has the same API as _http.Server_,77so we can do things like:7879 app.listen();80 app.listen(3000);8182### Route Parameters8384Previously we could use _this.param()_ to attempt85fetching a route, query string, or request body parameter:8687 get('/user/:id', function(){88 this.param('id');89 });9091Polymorphic parameter access can be done using `req.param()`:9293 app.get('/user/:id', function(req, res){94 req.param('id');95 });9697Route parameters are available via `req.params`:9899 app.get('/user/:id', function(req, res){100 req.params.id;101 });102103### Passing Route Control104105Old express had a weak notion of route passing,106which did not support async, and was never properly 107implemented for practical use:108109 get('/', function(){110 this.pass('/foobar');111 });112113Now Express has access to Connect's _next()_ function,114which is passed as the third and final argument. Calling _next()_ will115pass control to the next _matching route_, or continue down the stack116of Connect middleware.117118 app.get('/user/:id?', function(req, res, next){119 next();120 });121122 app.get('/user', function(){123 // ... respond124 });125126### View Rendering127128View filenames no longer take the form _NAME_._TYPE_._ENGINE_,129the _Content-Type_ can be set via _res.contentType()_ or130_res.header()_. For example what was previously _layout.html.haml_,131should now be _layout.haml_.132133Previously a view render looked something like this:134135 get('/', function(){136 this.render('index.html.haml', {137 locals: { title: 'My Site' }138 });139 });140141We now have _res.render()_, however the options passed to [haml](http://github.com/visionmedia/haml.js), [jade](http://github.com/visionmedia/jade), and others142remain the same.143144 app.get('/', function(req, res){145 res.render('index.haml', {146 locals: { title: 'My Site' }147 });148 });149150Previously rendering of a collection via _partial()_ would look something like this:151152 this.partial('comment.html.haml', { collection: comments });153154Although this worked just fine, it was generally to verbose, the similar but new API155looks like this, as _partial()_ is _always_ passed as a local variable:156157 partial('comment.haml', { collection: comments });158159To make things even less verbose we can assume the extension when omitted:160161 partial('comment', { collection: comments });162163And once again even further, when rendering a collection we can simply pass164an array, if no other options are desired:165166 partial('comments', comments);167168### Redirecting169170Previously you would171172 this.redirect('/somewhere');173174However you would now:175176 res.redirect('/somewhere');177 res.redirect('/somewhere', 301);178179### HTTP Client180181Previously Express provided a high level http client, this library is no more182as it does not belong in Express, however it may be resurrected as a separate module.183184### Core Extensions185186Express 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 to187roll your own, or install the module via:188189 $ npm install ext