migrate.md
This file
Path
docs/migrate.md
177 lines · 6.1 KiB · Markdown
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 80f4d08 point at it.
Release 2.0.0rcTj Holowaychukbefore adoption80f4d08
lines 1–123why, one step back12### Express 1.x to 2.x Migration34### HTTPS56 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 });4243### 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 // => false7778### res.cookie()7980 Previously only directly values could be passed, so for example:8182 res.cookie('rememberme', 'yes', { expires: new Date(Date.now() + 900000) });8384However 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 });8788### 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.103104### 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);123migration docs for partialsTj Holowaychukbefore adoptioncdbd8af
lines 124–146why, one step back124### partial() locals125126 Both _res.partial()_ and the _partial()_ functions accept an single object consisting of both the options and the locals. Previously with Express 1.x you may pass _user_ to a partial, along with _date_ like so:127 128 partial('user', { object: user, locals: { date: new Date }})129130or perhaps if you preferred not to use the inferred name _user_ you may used a local for this as well:131132 partial('user', { locals: { user: user, date: new Date }})133134 With recent changes to Express 2.x the object passed is now both, so the following is valid for the _object_ option and locals:135 136 partial('user', { object: user, date: new Date })137138 Or the following which is equivalent, however the local var name is explicitly set to _user_ instead of deduced from the filename.139 140 partial('user', { user: user, date: new Date })141142 When a "basic" object aka _{}_ or _new Object_ is passed, it is considered options, otherwise it is considered the _object_. The following are equivalent:143 144 partial('user', user); 145 partial('user', { object: user }); 146Release 2.0.0rcTj Holowaychukbefore adoption80f4d08
lines 147–177why, one step back147### Template Engine Compliance148149 To comply with Express previously engines needed the following signature:150 151 engine.render(str, options, function(err){});152153 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.154 155 var fn = engine.compile(str, options);156 fn(locals);157158### View Partial Lookup159160 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).161162### Mime Types163164 Express and Connect now utilize the _mime_ module in npm, so to add more use:165 166 require('mime').define({ 'foo/bar': ['foo', 'bar'] });167168### static() middleware169170 Previously named `staticProvider()`, the now `static()` middleware takes a single directory path, followed by options.171 172 app.use(express.static(__dirname + '/public', { maxAge: oneYear }));173174Previously when using options the `root` option would be used for this:175176 app.use(express.staticProvider({ root: __dirname + '/public', maxAge: oneYear }));177