migrate.md
This file
Path
docs/migrate.md
192 lines · 5.0 KiB · Markdown
Last changed by
Fixed typo in docsTj Holowaychukbefore adoption0a84824
History of this fileTake it
The walk stopped after 200 commits: lines older than dc77894 point at it.
Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894
line 1why, one step back1Started guideTj Holowaychukbefore adoption9e3a0c6
line 2why, one step back2### Built On ConnectMigration docs for req.param()Tj Holowaychukbefore adoptiondc77894
lines 3–4why, one step back34Express 1.x is written to run on-top of the [Connect](http://extjs.github.com/Connect) middlwareStarted guideTj Holowaychukbefore adoption9e3a0c6
lines 5–7why, one step back5framework, 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.Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894
lines 8–58why, one step back89### 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 back59 connect.cookieDecoder()Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894
lines 60–66why, one step back60 );6162or:6364 var connect = require('connect');65 var app = express.createServer();66Updated docsTj Holowaychukbefore adoptioncacada8
lines 67–69why, one step back67 app.use(connect.logger());68 app.use(connect.methodOverride());69 app.use(connect.cookieDecoder());Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894
lines 70–95why, one step back7071For 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()`:95Fixed migration typoTj Holowaychukbefore adoptiona9c3f56
line 96why, one step back96 app.get('/user/:id', function(req, res){Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894
lines 97–161why, one step back97 req.param('id');98 });99100Route parameters are also passed as the third argument:101102 app.get('/user/:id', function(req, res, params){103 params.id;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,117which is passed as the fourth and final argument. Calling _next()_ will118pass control to the next _matching route_, or continue down the stack119of Connect middleware.120121 app.get('/user/:id?', function(req, res, params, next){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 });161Added more view docsTj Holowaychukbefore adoption2f96506
line 162why, one step back162To make things even less verbose we can assume the extension when omitted:Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894
lines 163–170why, one step back163164 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);170Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7
line 171why, one step back171### RedirectingMigration docs for req.param()Tj Holowaychukbefore adoptiondc77894
lines 172–176why, one step back172173Previously you would174175 this.redirect('/somewhere');176Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7
line 177why, one step back177However you would now:Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894
lines 178–179why, one step back178179 res.redirect('/somewhere');Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7
line 180why, one step back180 res.redirect('/somewhere', 301);More migration docsTj Holowaychukbefore adoption38c453b
lines 181–192why, one step back181182### 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