Migrating from Expressjs 4 to 5

Written by amanhimself | Published 2016/12/29
Tech Story Tags: nodejs | javascript | expressjs | node | tech

TLDRvia the TL;DR App

With the start of 2017, it’s time to adapt these changes

ExpressJS 5.0 is in alpha release stage but I believe we’ll be adding it as a dependency in our package.json files in matter of no time. This article gives some tips regarding the way we are writing code using this framework and how we should adapt the new changes even if we are using ExpressJS version 4.0.

I’ll start with most common thing such as response .

Express 5 no longer supports the signature res.send , instead we should adapt using this method in this form:

res.status(statusCode).send();

We have to set the status code before sending the response object. This new version of res.send is basically a chain of two methods: res.status & res.send.

With that mind, ExpressJS 5 deprecates res.send(statusCode) method where statusCode is the number representing the HTTP response header status code. To send just the statusCode, that is, without sending the response object, we can use res.sendStatus(statusCode) method.

In similar manner, other methods that have been changed are:

res.json() --> res.status().json()res.jsonp() --> res.status.jsonp()

Another notable method that is going to be deprecated in next version of ExpressJS is res.sendfile() . Instead, we must adapt its new form, the camelCase one: res.sendFile() which is already been supported by the ExpressJS versions later than 4.8.x . It comes with optional parameters that you can check them here.

Whether you are planning to use the alpha release of Express 5.0 or still going on with the latest versions of Express 4.0, I would suggest to start adapting these methods immediately.

The full list of changes or the official Express Migration Guide is to be found here.

Thank you for reading. If you find this post useful, please hit the 💚 button so this story can reach out to more readers_. If you’d like to talk about it more, ping me on_ Twitter | Goodreads | Book Blog | Website | My articles on Hackernoon.com


Written by amanhimself | Developer, Tech Writer | React Native & Expo enthusiast | Personal blog: amanhimself.dev
Published by HackerNoon on 2016/12/29