Expressjs is a popular framework for nodejs, you can say its foundation of nodejs. The current version is express 4 which is the most popular and stable version and work on next version Express5 was being done but super slow.
Express5 is finally released after wait of 10 years with some major changes. In this article we are going to cover all these changes.
If you prefers to watch videos, i have created a video with proper explanation and comparision with expressjs4.
Here are 10 major changes in express 5.
1. Support for es6
Express5 now supports es6 which means we can use import instead of require and all other modern ecmascript features. This allows developers to write much cleaner code and use all modern javascript syntax.
2. Promise rejection support
In express4 if you are using promise/async await code and if error happens, you had to manually catch the exception and then pass the error in next middleware.
Express5 automatically handles this which means focus on your logic not handling the exception.
3. Builtin body parser
In Express 4 we had to install seperate library “body-parser” for handling post data.
Express 5 has body-parser integrated internally which means no extra dependency.
4. req.param(name) removed
In earlier version of express — you could extract forms data using
req.param(name)
This could be data from query, param or even body which confuses the source of data. This was already deprecated.
But in express it has been completely removed. Now you need to explicitly use — req.query
, req.body
or req.param
based on where data is coming from.
5. app.del replaced with app.delete
In express 4 — the HTTP DELETE route was registered using app.del but in express5 it has been replaced with more explicit word delete, so you need to now use app.delete
6. app.param(fn) middleware is removed
In express4 app.param middleware was used to defined middleware that executes whenever the route param was found. This was used to preprocess some data, for example — if a route param has userId then find the user from database and attach to the request which would be needed later.
In express5 this has been removed and developers are encouraged to use more explicit approach that is use a middleware just before the route. This makes the things much clear and easy to debug.
7. res.send(status)
replaced with res.sendStatus(status)
In express 4 you could send a response without data using res.send which confused developers like where is the data, is it a bug or something different.
In express5 an explicit method res.sendStatus is introduced for sending response with only status code and no data.
8. res.send
sends data only
In express 4, you could send response with status code and data using
res.send(stauts, data);
but in express5, the send method only accepts one parameter which is data and a method status has been added to send the status.
res.status(status).send(data);
9. req.query
is read only
In express 4 , you could mutate the req.query — add aadditional property or remove.
req.query.id =5 ;
this creates confusion as source of data was not request but set somewhere explicitly in the code.
In express 5, req.query
is read only which means no more changes on the req.query
10. req.body
is undefined by default
In express 4, req.body value was empty object {}by default. So if you are in get request or you have not configured body parser the value of req.body will be empty object.
But in express5, it will be undefined. Which will explicitly states that there is no body data.
Conclusion
Express 5 majorly focuses on -
- Express 5 focuses on making things more clear and explicitly defined.
- Remove confusing parts
- Move external dependency to internal dependency
- Internal optimization
- Support for modern javascript
👉 Thanks for reading everyone. Please let me know what do you think about express5 in the comments and follow me for more such articles.
💁♂ Also if you like to watch tech videos, please subscribe to my youtube channel. Where i share lot of tech tips, news, experiment etc.
https://www.youtube.com/@codewithujjwalgupta
Connect with me at -
Twitter — https://twitter.com/ujjwal_kr_gupta
Linkedin — https://www.linkedin.com/in/ujjwalkrgupta/
Top comments (0)