migrate.md
This file
Path
docs/migrate.md
192 lines · 5.0 KiB · Markdown
Last changed by
Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7
History of this fileTake it
Added migrate doc filesTj Holowaychukbefore adoption87bb27c
line 1why, one step back1Started guideTj Holowaychukbefore adoption9e3a0c6
line 2why, one step back2### Built On ConnectAdded migrate doc filesTj Holowaychukbefore adoption87bb27c
line 3why, one step back3Started migration docsTj Holowaychukbefore adoption9e68a5b
line 4why, one step back4Express 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.Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 8–25why, 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, andRemoved backticks from docsTj Holowaychukbefore adoptionc3f71b3
lines 26–27why, one step back26introduce _express.createServer()_ which accepts the same arguments27as _http.createServer()_:Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 28–39why, one step back2829 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 });39Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7
lines 40–51why, one step back40Express 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);51Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 52why, one step back52Which we can now _use()_ within our app, or pass to the _express.createServer()_ method:Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7
lines 53–68why, one step back5354 var connect = require('connect');5556 var app = express.createServer(57 connect.logger(),58 connect.methodOverride(),59 connect.cookie()60 );6162or:6364 var connect = require('connect');65 var app = express.createServer();6667 app.use('/', connect.logger());68 app.use('/', connect.methodOverride());Added configuration sectionTj Holowaychukbefore adoption83d2555
line 69why, one step back69 app.use('/', connect.cookieDecoder());Docs for plugins vs middlewareTj Holowaychukbefore adoption927aae7
lines 70–71why, one step back7071For documentation on creating Connect middleware visit [Middleware Authoring](http://extjs.github.com/Connect/#Middleware-Authoring).Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 72–74why, one step back7273### Running Applications74Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 75why, one step back75Previously a global function _run()_, was available:Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 76–78why, one step back7677 run();78Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 79why, one step back79The new _express.Server_ has the same API as _http.Server_,Started migration docsTj Holowaychukbefore adoption9e68a5b
lines 80–83why, one step back80so we can do things like:8182 app.listen();83 app.listen(3000);Migration docs for paramsTj Holowaychukbefore adoptionda84c0b
lines 84–86why, one step back8485### Route Parameters86Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 87why, one step back87Previously we could use _this.param()_ to attemptMigration docs for paramsTj Holowaychukbefore adoptionda84c0b
lines 88–93why, one step back88fetching a route, query string, or request body parameter:8990 get('/user/:id', function(){91 this.param('id');92 });93Migration docs for req.param()Tj Holowaychukbefore adoptiondc77894
lines 94–100why, one step back94Polymorphic parameter access can be done using `req.param()`:9596 app.get('/user/:id', function(){97 req.param('id');98 });99100Route parameters are also passed as the third argument:Migration docs for paramsTj Holowaychukbefore adoptionda84c0b
lines 101–105why, one step back101102 app.get('/user/:id', function(req, res, params){103 params.id;104 });105Control passing docsTj Holowaychukbefore adoption548a070
lines 106–115why, one step back106### 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 });115Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
lines 116–117why, one step back116Now Express has access to Connect's _next()_ function,117which is passed as the fourth and final argument. Calling _next()_ willControl passing docsTj Holowaychukbefore adoption548a070
lines 118–126why, one step back118pass 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 // ... respondAdded view rendering migration sectionTj Holowaychukbefore adoption0b86ce2
lines 127–131why, one step back127 });128129### View Rendering130131View filenames no longer take the form _NAME_._TYPE_._ENGINE_,Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
lines 132–133why, one step back132the _Content-Type_ can be set via _res.contentType()_ or133_res.header()_. For example what was previously _layout.html.haml_,Added view rendering migration sectionTj Holowaychukbefore adoption0b86ce2
lines 134–143why, one step back134should 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 });143Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 144why, one step back144We now have _res.render()_, however the options passed to [haml](http://github.com/visionmedia/haml.js), [jade](http://github.com/visionmedia/jade), and othersAdded view rendering migration sectionTj Holowaychukbefore adoption0b86ce2
lines 145–150why, one step back145remain the same.146147 app.get('/', function(req, res){148 res.render('index.haml', {149 locals: { title: 'My Site' }150 });Control passing docsTj Holowaychukbefore adoption548a070
line 151why, one step back151 });Added partial migration docsTj Holowaychukbefore adoption2a674f4
line 152why, one step back152Removed backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 153why, one step back153Previously rendering of a collection via _partial()_ would look something like this:Added partial migration docsTj Holowaychukbefore adoption2a674f4
lines 154–157why, one step back154155 this.partial('comment.html.haml', { collection: comments });156157Although this worked just fine, it was generally to verbose, the similar but new APIRemoved backticks from docsTj Holowaychukbefore adoptionc3f71b3
line 158why, one step back158looks like this, as _partial()_ is _always_ passed as a local variable:Added partial migration docsTj Holowaychukbefore adoption2a674f4
lines 159–169why, one step back159160 partial('comment.haml', { collection: comments });161162To make things even less verbose we can assume the extension when ommitted:163164 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);Added redirect migration docsTj Holowaychukbefore adoption74432a6
line 170why, one step back170Merge branch 'redirect' into connectTj Holowaychukbefore adoption87cfca7
line 171why, one step back171### RedirectingAdded redirect migration docsTj Holowaychukbefore adoption74432a6
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:Added redirect migration docsTj Holowaychukbefore adoption74432a6
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