Skip to content
Myr

migrate.md

This file

Path

docs/migrate.md

143 lines · 4.7 KiB · Markdown

Last changed by

docs for res.render() and res.partial()Tj Holowaychukbefore adoption07ce5d3

History of this file

Take it

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

Fixed express(1) support for < 0.3.xTj Holowaychukbefore adoption544d688

line 1why, one step back
1

docsTj Holowaychukbefore adoption4609984

line 2why, one step back
2### Express 1.x to 2.x Migration

Fixed express(1) support for < 0.3.xTj Holowaychukbefore adoption544d688

line 3why, one step back
3

docsTj Holowaychukbefore adoptionc1a82bf

lines 4–41why, one step back
4### HTTPS
56 Creating an HTTPS server is simply, simply pass the TLS options to _express.createServer()_:7 8 var app = express.createServer({9 key: ...10 , cert: ...11 });1213 app.listen(443);1415### req.header() Referrer1617 Previously if anyone was doing something similar to:18 19 req.headers.referrer || req.headers.referer20 req.header('Referrer') || req.header('Referer')2122 With the new special-case we may now simply use _Referrer_ which will return either if defined:23 24 req.header('Referrer')2526### res.local(name, val)2728 Previously all local variables had to be passed to _res.render()_, or either _app.helpers()_ or _app.dynamicHelpers()_, now we may do this at the request-level progressively. The _res.local()_ method accepts a _name_ and _val_, however the locals passed to _res.render()_ will take precedence.2930 For example we may utilize this feature to create locals in middleware:3132 function loadUser(req, res, next) {33 User.get(req.params.id, function(err, user){34 res.local('user', user);35 next();36 });37 }3839 app.get('/user/:id', loadUser, function(req, res){40 res.render('user');41 });

docsTj Holowaychukbefore adoption1db2efa

lines 42–77why, one step back
42
43### req.param(name[, defaultValue])4445 Previously only _name_ was accepted, so some of you may have been doing the following:46 47 var id = req.param('id') || req.user.id;4849 The new _defaultValue_ argument can handle this nicely:50 51 var id = req.param('id', req.user.id);5253### app.helpers() / app.locals()5455 _app.locals()_ is now an alias of _app.helpers()_, as helpers makes more sense for functions.5657### req.accepts(type)5859 _req.accepts()_ now accepts extensions:60 61 62 // Accept: text/html63 req.accepts('html');64 req.accepts('.html');65 // => true66 67 // Accept: text/*; application/json68 req.accepts('html');69 req.accepts('text/*');70 req.accepts('text/plain');71 req.accepts('application/json');72 // => true73 74 req.accepts('image/png');75 req.accepts('png');76 // => false77

docs for res.cookie()Tj Holowaychukbefore adoption00e1dbf

lines 78–81why, one step back
78### res.cookie()
7980 Previously only directly values could be passed, so for example:81

docs for res.render() and res.partial()Tj Holowaychukbefore adoption07ce5d3

line 82why, one step back
82 res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000) });

docs for res.cookie()Tj Holowaychukbefore adoption00e1dbf

lines 83–87why, one step back
83
84However now we have the alternative _maxAge_ property which may be used to set _expires_ relative to _Date.now()_ in milliseconds, so our example above can now become:8586 res.cookie('rememberme', 'yes', { maxAge: 900000 });87

docsTj Holowaychukbefore adoption1db2efa

lines 88–103why, one step back
88### res.download() / res.sendfile()
8990 Both of these methods now utilize Connect's static file server behind the scenes (actually the previous Express code was ported to Connect 1.0). With this change comes a change to the callback as well. Previously the _path_ and _stream_ were passed, however now only an _error_ is passed, when no error has occurred the callback will be invoked indicating that the file transfer is complete. The callback remains optional:91 92 res.download('/path/to/file');9394 res.download('/path/to/file', function(err){95 if (err) {96 console.error(err);97 } else {98 console.log('transferred');99 }100 });101102 The _stream threshold_ setting was removed.103

docs for res.render() and res.partial()Tj Holowaychukbefore adoption07ce5d3

lines 104–124why, one step back
104### res.render()
105106 Previously locals were passed as a separate key:107 108 res.render('user', { layout: false, locals: { user: user }});109110 In Express 2.0 both the locals and the options are one in the same, meaning you cannot have a local variable named _layout_ as it is reserved for express, however this cleans up the API:111 112 res.render('user', { layout: false, user: user });113114### res.partial()115116 Express 2.0 adds the _res.partial()_ method, helpful for rendering partial fragments over WebSockets or Ajax requests etc. The API is identical to the _partial()_ calls within views.117 118 // render a collection of comments119 res.partial('comment', [comment1, comment2]); 120121 // render a single comment122 res.partial('comment', comment);123124### Template Engine Compliance

docsTj Holowaychukbefore adoption1db2efa

lines 125–143why, one step back
125
126 To comply with Express previously engines needed the following signature:127 128 engine.render(str, options, function(err){});129130 Now they must export a _compile()_ function, returning a function which when called with local variables will render the template. This allows Express to cache the compiled function in memory during production.131 132 var fn = engine.compile(str, options);133 fn(locals);134135### View Partial Lookup136137 Previously partials were loaded relative to the now removed _view partials_ directory setting, or by default _views/partials_, now they are relative to the view calling them, read more on [view lookup](guide.html#View-Lookup).138139### Mime Types140141 Express and Connect now utilize the _mime_ module in npm, so to add more use:142 143 require('mime').define({ 'foo/bar': ['foo', 'bar'] });