Skip to content
Myr

migrate.1

This file

Path

docs/migrate.1

272 lines · 5.4 KiB

Last changed by

docsTj Holowaychukbefore adoption2bb0412

History of this file

Take it

1.\" generated with Ronn/v0.7.32.\" http://github.com/rtomayko/ronn/tree/0.7.33.4.TH "MIGRATE" "" "March 2011" "" ""5.6.SH "NAME"7\fBmigrate\fR8.9.SS "Express 1\.x to 2\.x Migration"10.11.SS "HTTPS"12Creating an HTTPS server is simply, simply pass the TLS options to \fIexpress\.createServer()\fR:13.14.IP "" 415.16.nf1718 var app = express\.createServer({19     key: \.\.\.20   , cert: \.\.\.21 });2223 app\.listen(443);24.25.fi26.27.IP "" 028.29.SS "req\.header() Referrer"30Previously if anyone was doing something similar to:31.32.IP "" 433.34.nf3536 req\.headers\.referrer || req\.headers\.referer37 req\.header(\'Referrer\') || req\.header(\'Referer\')38.39.fi40.41.IP "" 042.43.P44With the new special\-case we may now simply use \fIReferrer\fR which will return either if defined:45.46.IP "" 447.48.nf4950 req\.header(\'Referrer\')51.52.fi53.54.IP "" 055.56.SS "res\.local(name, val)"57Previously all local variables had to be passed to \fIres\.render()\fR, or either \fIapp\.helpers()\fR or \fIapp\.dynamicHelpers()\fR, now we may do this at the request\-level progressively\. The \fIres\.local()\fR method accepts a \fIname\fR and \fIval\fR, however the locals passed to \fIres\.render()\fR will take precedence\.58.59.P60For example we may utilize this feature to create locals in middleware:61.62.IP "" 463.64.nf6566 function loadUser(req, res, next) {67   User\.get(req\.params\.id, function(err, user){68     res\.local(\'user\', user);69     next();70   });71 }7273 app\.get(\'/user/:id\', loadUser, function(req, res){74   res\.render(\'user\');75 });76.77.fi78.79.IP "" 080.81.SS "req\.param(name[, defaultValue])"82Previously only \fIname\fR was accepted, so some of you may have been doing the following:83.84.IP "" 485.86.nf8788 var id = req\.param(\'id\') || req\.user\.id;89.90.fi91.92.IP "" 093.94.P95The new \fIdefaultValue\fR argument can handle this nicely:96.97.IP "" 498.99.nf100101 var id = req\.param(\'id\', req\.user\.id);102.103.fi104.105.IP "" 0106.107.SS "app\.helpers() / app\.locals()"108\fIapp\.locals()\fR is now an alias of \fIapp\.helpers()\fR, as helpers makes more sense for functions\.109.110.SS "req\.accepts(type)"111\fIreq\.accepts()\fR now accepts extensions:112.113.IP "" 4114.115.nf116117  // Accept: text/html118  req\.accepts(\'html\');119  req\.accepts(\'\.html\');120  // => true121122  // Accept: text/*; application/json123  req\.accepts(\'html\');124  req\.accepts(\'text/*\');125  req\.accepts(\'text/plain\');126  req\.accepts(\'application/json\');127  // => true128129  req\.accepts(\'image/png\');130  req\.accepts(\'png\');131  // => false132.133.fi134.135.IP "" 0136.137.SS "res\.cookie()"138Previously only directly values could be passed, so for example:139.140.IP "" 4141.142.nf143144res\.cookie(\'rememberme\', \'yes\', { expires: new Date(Date\.now() + 900000) });145.146.fi147.148.IP "" 0149.150.P151However now we have the alternative \fImaxAge\fR property which may be used to set \fIexpires\fR relative to \fIDate\.now()\fR in milliseconds, so our example above can now become:152.153.IP "" 4154.155.nf156157res\.cookie(\'rememberme\', \'yes\', { maxAge: 900000 });158.159.fi160.161.IP "" 0162.163.SS "res\.download() / res\.sendfile()"164Both 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 \fIpath\fR and \fIstream\fR were passed, however now only an \fIerror\fR is passed, when no error has occurred the callback will be invoked indicating that the file transfer is complete\. The callback remains optional:165.166.IP "" 4167.168.nf169170 res\.download(\'/path/to/file\');171172 res\.download(\'/path/to/file\', function(err){173   if (err) {174     console\.error(err);175   } else {176     console\.log(\'transferred\');177   }178 });179.180.fi181.182.IP "" 0183.184.P185The \fIstream threshold\fR setting was removed\.186.187.SS "res\.render()"188Previously locals were passed as a separate key:189.190.IP "" 4191.192.nf193194 res\.render(\'user\', { layout: false, locals: { user: user }});195.196.fi197.198.IP "" 0199.200.P201In Express 2\.0 both the locals and the options are one in the same, meaning you cannot have a local variable named \fIlayout\fR as it is reserved for express, however this cleans up the API:202.203.IP "" 4204.205.nf206207 res\.render(\'user\', { layout: false, user: user });208.209.fi210.211.IP "" 0212.213.SS "res\.partial()"214Express 2\.0 adds the \fIres\.partial()\fR method, helpful for rendering partial fragments over WebSockets or Ajax requests etc\. The API is identical to the \fIpartial()\fR calls within views\.215.216.IP "" 4217.218.nf219220 // render a collection of comments221 res\.partial(\'comment\', [comment1, comment2]);222223 // render a single comment224 res\.partial(\'comment\', comment);225.226.fi227.228.IP "" 0229.230.SS "Template Engine Compliance"231To comply with Express previously engines needed the following signature:232.233.IP "" 4234.235.nf236237 engine\.render(str, options, function(err){});238.239.fi240.241.IP "" 0242.243.P244Now they must export a \fIcompile()\fR 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\.245.246.IP "" 4247.248.nf249250 var fn = engine\.compile(str, options);251 fn(locals);252.253.fi254.255.IP "" 0256.257.SS "View Partial Lookup"258Previously partials were loaded relative to the now removed \fIview partials\fR directory setting, or by default \fIviews/partials\fR, now they are relative to the view calling them, read more on view lookup \fIguide\.html#View\-Lookup\fR\.259.260.SS "Mime Types"261Express and Connect now utilize the \fImime\fR module in npm, so to add more use:262.263.IP "" 4264.265.nf266267 require(\'mime\')\.define({ \'foo/bar\': [\'foo\', \'bar\'] });268.269.fi270.271.IP "" 0272