nest icon indicating copy to clipboard operation
nest copied to clipboard

Express 5 support

Open TrejGun opened this issue 1 year ago • 1 comments

Is there an existing issue that is already proposing this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe it

As we are all aware Express 5 has been in beta for a while. I would like to clarify the developers' position on potential migration and define the scope.

Here are the breaking changes introduced in Express.js v5, with additional details and corrections based on the latest documentation:

Route Param Middleware

  • Error Propagation: If next is called with an error in route parameter middleware, it now skips any remaining param middleware and goes straight to error handling, making it more predictable.
app.param('id', (req, res, next, id) => {
  if (isNaN(id)) return next(new Error('ID must be a number'));
  next();
});

app.param('id', (req, res, next, id) => {
  console.log('This will not run if there’s an error in the first handler');
  next();
});

Removed Deprecated Methods

  • Methods like res.sendfile(), res.send(status), and res.json(obj, status) have been removed. Instead, use the camel-cased res.sendFile(), and chain res.status() for setting status codes.
// Old way (v4)
res.json({ message: "success" }, 200);

// New way (v5)
res.status(200).json({ message: "success" });

Improved Error Handling for Async Functions

  • Express 5 automatically forwards errors from async route handlers and middleware to the error-handling middleware. This makes handling errors in asynchronous code much simpler.
// v5 async error handling
app.get('/async-route', async (req, res, next) => {
  throw new Error('This will be caught by the error handler');
});

Changes in Path Route Matching

  • Express 5 introduces stricter route matching rules. For example, wildcard patterns like (*) are no longer valid and must be written as (.*). Additionally, new modifiers like ?, *, and + have been added to parameter patterns.
// Express 4 (Valid)
app.get('/users/:id(\\d+)', (req, res) => { ... });

// Express 5 (Updated syntax)
app.get('/users/:id(\\d+)', (req, res) => { ... });

HTTP/2 Support

Express v5 includes native support for HTTP/2, which allows for better performance and the use of modern features such as multiplexing and server push.

Enforcement of Asynchronous View Rendering

The res.render() method now enforces asynchronous behavior for all view engines, ensuring consistency and avoiding bugs caused by synchronous implementations.

Describe the solution you'd like

native support for express 5 is implemented

Teachability, documentation, adoption, migration strategy

https://expressjs.com/en/guide/migrating-5.html

What is the motivation / use case for changing the behavior?

i would like to utilize new features of ExpressJS 5 like http2 support

TrejGun avatar Sep 27 '24 04:09 TrejGun

I would like to contribute to this enhancement

kalyan90 avatar Oct 08 '24 14:10 kalyan90

https://github.com/nestjs/nest/pull/14177

kamilmysliwiec avatar Nov 20 '24 14:11 kamilmysliwiec