
Introduction
Express 5.0 is currently in the alpha release stage and it will not be quite different from Express 4. While the underlying API remains the same as that of Express 4, you may need to watch for some deprecated methods that may break your application when you upgrade.
It should, however, be a smooth transition between Express v4 and Express v5 as we will see in the following examples.
We’ve built APIs on Express 4, so we’re very excited to see that 5 is coming shortly
To get started, set up a new Node.js project with npm init and install the latest Express 5 alpha release from npm. You can always refer to the release logs from the Express repository’s history.md file.
- npm install express@5.0.0-alpha.3 –save
We are now ready to see some of the changes. Most of the methods and properties that we will look at here were previously deprecated and have been completely removed in Express 5.
app.del()
app.del() has been removed as a HTTP DELETE registration method in favor of app.delete().
app.del(‘/resource’, (req, res) => res.send(‘deleted’));
app.param(fn)
The app.param(fn) signature was used for modifying the behavior of the app.param(name, fn) function. It has been deprecated since v4.11.0, and Express 5 no longer supports it at all.
req.param(name)
You can no longer access request parameters using the param method. Instead, use the req.params, req.body, or req.query objects.
app.get(‘/users/:id’, (req, res) => { res.send(User.find(req.param(‘id’))); }); app.get(‘/user/:id’, (req, res) => { res.send(User.find(req.params.id)); });
The following methods have been pluralized in Express 5:
- req.acceptsCharset() is replaced by req.acceptsCharsets().
- req.acceptsEncoding() is replaced by req.acceptsEncodings().
- req.acceptsLanguage() is replaced by req.acceptsLanguages().
The res.sendfile() function has been replaced by a camel-cased version res.sendFile().
res.json(obj, status)
Express 5 no longer supports the signature res.json(obj, status). Instead, set the status and then chain it to the res.json() method like:
res.status(status).json(obj)
res.jsonp(obj, status)
Express 5 no longer supports the signature res.jsonp(obj, status). Instead, set the status and then chain it to the res.jsonp() method like:
res.status(status).jsonp(obj)
res.send(body, status)
Express 5 no longer supports the signature res.send(obj, status). Instead, set the status and then chain it to the res.send() method like:
res.status(status).send(obj)
If your first experience with the micro framework was Express 4, you probably have no idea what this is all about.
A leading colon character (:) in the name for the app.param(name, fn) function is a remnant of Express 3, and for the sake of backward compatibility, Express 4 supported it with a deprecation notice. Express 5 will silently ignore it and use the name parameter without prefixing it with a colon.
This should not affect your code if you follow the Express 4 documentation of app.param, as it makes no mention of the leading colon.
In Express 4, res.send(status) where status is a number was deprecated as a way of setting the HTTP status code header in favor of res.sendStatus(statusCode).
app.get(‘/’, (req, res) => { res.send(500); });
This is the expected response in Express 4 (500 Internal Server Error)
- http :8001
Output
HTTP/1.1 500 Internal Server Error Connection: keep-alive Content-Length: 21 Content-Type: text/plain; charset=utf-8 Date: Wed, 08 Feb 2017 12:01:48 GMT ETag: W/”15-3JQVFLwoG6yepWGqlDPA/A” X-Powered-By: Express Internal Server Error
The same code would return the exact opposite in Express 5 (200 status code with ‘500’ message).
- http :8000
Output
HTTP/1.1 200 OK Connection: keep-alive Content-Length: 3 Content-Type: application/json; charset=utf-8 Date: Wed, 08 Feb 2017 12:04:22 GMT ETag: W/”3-zuYxEhwuySMvOi8CitXImw” X-Powered-By: Express 500
Express 5 no longer supports the signature res.send(status), where status is a number. Instead, use the res.sendStatus(statusCode) function, which sets the HTTP response header status code and sends the text version of the code: “Not Found”, “Internal Server Error”, and so on.
If you need to send a number by using the res.send() function, quote the number to convert it to a string, so that Express does not interpret it as an attempt to use the unsupported old signature.
app.router
The app.router object, which was removed in Express 4, has made a comeback in Express 5. In the new version, this object is just a reference to the base Express router, unlike in Express 3, where an app had to explicitly load it.
req.host
In Express 4, the req.host function incorrectly stripped off the port number if it was present. In Express 5 the port number is maintained.
app.get(‘/’, (req, res) => { res.status(200).send(req.host); });
req.query
In Express 4.7 and Express 5 onwards, the query parser option can accept false to disable query string parsing when you want to use your own function for query string parsing logic.
res.render()
This method now enforces asynchronous behavior for all view engines, avoiding bugs caused by view engines that had a synchronous implementation and that violated the recommended interface.
Read more about this in this Express GitHub issue.
Middleware Promises
Promises have become quite popular in asynchronous development and it’s been proposed that next() returns a promise so that middleware can be resolved and propagated through the next method.
This is how that would look like.
app.use(function (req, res, next) { return User.get(req.session.userid).then(function (user) { req.user = user }) .then(next) .then(function () { res.send(user) }) })
Express 5 is still in alpha so there are bound to be changes. I will keep updating this post as the updates are rolled out. Take a look at this pull request if you want to see an updated list of changes for release.