The arrival of Express.js v5 marks a decade of development!

Posted by


Express.js has been a popular web application framework for Node.js for over a decade, and now it has finally released its latest version, v5. This new version comes with some major improvements and changes that make it even more powerful and flexible. In this tutorial, we will walk you through the new features and changes in Express.js v5 and show you how to get started with this latest version.

  1. Installation
    To install Express.js v5, you can use npm, the Node.js package manager. Open your terminal and run the following command:
npm install express@next

This will install the latest version of Express.js, which is currently v5. You can also specify the exact version by running:

npm install express@5.0.0-alpha.1
  1. Major Changes
    Express.js v5 comes with some major changes and improvements, including:
  • Removal of the built-in middleware: In previous versions of Express.js, there were built-in middleware functions that were automatically added to every route. In v5, these middleware functions have been removed, and you need to explicitly add them to your routes if you want to use them.
  • Improved error handling: Error handling has been restructured in v5, making it easier and more flexible to handle errors in your application.
  • Enhanced routing capabilities: The routing system in Express.js has been improved in v5, allowing for more complex and flexible routing configurations.
  1. Getting Started
    To get started with Express.js v5, create a new Node.js project and install Express.js using npm as described in the installation section above. Create a new file called app.js and add the following code to set up a basic Express.js server:
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

Run the following command in your terminal to start the server:

node app.js

You should see a message in the terminal saying "Server running at http://localhost:3000". Open your web browser and navigate to http://localhost:3000 to see the "Hello World!" message displayed on the page.

  1. Express.js Middleware
    Middleware functions are at the core of Express.js, allowing you to intercept and modify incoming requests and outgoing responses. In Express.js v5, you need to explicitly add middleware functions to your routes. Here’s an example of how to add a middleware function to a route:
const express = require('express');
const app = express();
const port = 3000;

// Custom middleware function
app.use((req, res, next) => {
  console.log('Middleware function triggered');
  next();
});

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

In the example above, the custom middleware function is added using app.use() and will be triggered for every incoming request. You can also add middleware functions to specific routes using app.use('/myroute', myMiddleware).

  1. Error Handling
    Error handling in Express.js v5 has been improved and restructured to make it easier to handle and manage errors in your application. You can use the next() function to pass errors to the error handling middleware, as shown in the example below:
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  const error = new Error('Error message');
  next(error);
});

app.use((err, req, res, next) => {
  res.status(500).send('Internal Server Error');
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

In the example above, an error is created within the route handler, and then passed to the error handling middleware using next(error). The error handling middleware then responds with a 500 status code and an error message.

  1. Conclusion
    Express.js v5 brings significant improvements and changes to the popular web application framework for Node.js. The removal of built-in middleware, improved error handling, and enhanced routing capabilities make Express.js even more powerful and flexible. This tutorial has shown you how to get started with Express.js v5 and use some of its new features. Express.js v5 is definitely worth exploring for both new and existing Express.js developers.
0 0 votes
Article Rating

Leave a Reply

19 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@jacobwwarner
1 hour ago

Would be interested in your thoughts on fastify.

@amansagar4948
1 hour ago

Man, software is seriously overwhelming. 10 whole years damn. I request every indian software engineer to stick around and don’t be sad in the short run if you’re finding it difficult, software will reward you with time, it’s beautiful

@thehungrybird
1 hour ago

It would be great if you share the links in description.

@bgspradeep
1 hour ago

Indians and their obsession with foreign softwares , peak pajeet behaviour

@yassinesafraoui
1 hour ago

this is like when chihiro washed all the mud off of that monster in spirited away

@runaway09
1 hour ago

Express is back baby!

@4v4
1 hour ago

who needs express when you have hono

@binarybulletin
1 hour ago

Next js not harmed this change release v5

@codedusting
1 hour ago

Chad library. Didn't do much except security fixes for the last 10 years and even then remained the top used backend node framework. Now moving again. Pretty sure they'll work for at max 2 years, incorporate all the good learninga from others, make it extremely fast and sit back for another decade 😂

Not to mention, the extensibility and stability of it.

@albinopepegas8391
1 hour ago

We have express v5 before GTA 6

@elvispalace
1 hour ago

its still using CommonJS

@Khawar_Mehfooz
1 hour ago

Took it 10 years cause the maintainers are busy closing spam PRs😂

@akshayyadav5914
1 hour ago

they spend 10 years to block all the spam, that shradha khapraa created on their repo

@sourabhsingh4515
1 hour ago

not saying that express is bad but it is pretty much the starting point of every student and then they don't want to move away from their comfort zone and there isn't much awareness about frameworks like hono and their capabilities same with bun and node js although bun is way better than node js but still there isn't much awareness about it and most of the people choose their starting point with npm and node that is why the download number is still low and hono is just release in 2022 so there is no chance that it can beat express in download numbers but the case is somewhat opposite in terms of features and performance.

@sunnyy6295
1 hour ago

Express should look upon nestjs and give Inbuilt Microservices, sockets, cronjob functions, etc

@imPrathamDev
1 hour ago

In the first video I was thinking why mahul explaining node js article again 😅

@LarsRyeJeppesen
1 hour ago

This is good. Remove old s…

@kaushal-krishna
1 hour ago

Only Legends know it's a re-upload 😂

@omersoncruz1081
1 hour ago

First… 😀

19
0
Would love your thoughts, please comment.x
()
x