2023-02-24
2421
#node
Standard Liv
3694
Feb 24, 2023 ⋅ 8 min read

Forget Express.js — opt for these alternatives instead

Standard Liv I'm a software engineer living in the Bay Area. she/her

Recent posts:

Nx Adoption Guide: Overview, Examples, And Alternatives

Nx adoption guide: Overview, examples, and alternatives

Let’s explore Nx features, use cases, alternatives, and more to help you assess whether it’s the right tool for your needs.

Andrew Evans
Mar 28, 2024 ⋅ 9 min read
Understanding Security In React Native Applications

Understanding security in React Native applications

Explore the various security threats facing React Native mobile applications and how to mitigate them.

Wisdom Ekpotu
Mar 27, 2024 ⋅ 10 min read
Warp Adoption Guide: Overview, Examples, And Alternatives

warp adoption guide: Overview, examples, and alternatives

The warp web framework for Rust offers many enticing features. Let’s see when and why you should consider using warp in your projects.

Ukeje Goodness
Mar 26, 2024 ⋅ 8 min read
Integrating Next Js And Signalr For Enhanced Real Time Web App Capabilities

Integrating Next.js and SignalR to build real-time web apps

In this tutorial, you’ll learn how to integrate Next.js and SignalR to build an enhanced real-time web application.

Clara Ekekenta
Mar 25, 2024 ⋅ 8 min read
View all posts

10 Replies to "Forget Express.js — opt for these alternatives instead"

  1. Fastify is on to something. Having request/response validation built in is such a nice thing to have standardized.

    However Express middleware can be an async function out of the box. No idea why an asyncHandler method even exists.

  2. I recommend NestJS for an Enterprise level node framework. It is the most fun I’ve had developing a node backend. Moreover it supports either express or fastify as middleware out of the box.

  3. Sails is waaaay better. Almost as good as rails in terms of code brevity, but much faster performance of node.

  4. Just a quick note, express does work well with async/await out of the box! The wrapper you are using (express-async-handler) is just a workaround to abstract away error handling. Otherwise, you can just use try/catch just as you other examples, without any need for this extra dependency.

  5. You don’t have to use express-async-handler to use async function as the middleware. Try it for yourself by removing it. As far as I see, it does not provide much value.

  6. @fred yang

    I recommend the middleware-async package instead.

    https://www.npmjs.com/package/middleware-async

    If you are going to use async function as a middleware. I highly recommend you wrap it by a helper function, such as middleware-async. (It is well tested and I use it in many production projects). There are also handy helper functions combineMiddlewares, middlewareToPromise, combineToAsync, which are very useful in testing.

    Code 1: no async, error caught.
    Code 2: async, error not caught. The connection hangs until the client stops it.
    Code 3: async, wrapped with middleware-async. Error caught
    Code 3: no async, wrapped with middleware-async. Error caught

    Code 1:

    const app = require(‘express’)()

    app.get(‘/’, (req, res, next) => {
    throw new Error(‘xx’)
    res.send(‘hi’)
    })
    app.use((err, req, res, next) => {
    console.error(err)
    res.send(‘error’)
    })
    app.listen(3000)

    Code 2:

    const app = require(‘express’)()

    app.get(‘/’, async (req, res, next) => {
    throw new Error(‘xx’)
    res.send(‘hi’)
    })
    app.use((err, req, res, next) => {
    console.error(err)
    res.send(‘error’)
    })
    app.listen(3000)

    Code 3:

    const app = require(‘express’)()
    const {asyncMiddleware} = require(‘middleware-async’)

    app.get(‘/’, asyncMiddleware(async (req, res, next) => {
    throw new Error(‘xx’)
    res.send(‘hi’)
    }))
    app.use((err, req, res, next) => {
    console.error(err)
    res.send(‘error’)
    })
    app.listen(3000)

    Code 4:

    const app = require(‘express’)()
    const {asyncMiddleware} = require(‘middleware-async’)

    app.get(‘/’, asyncMiddleware((req, res, next) => {
    throw new Error(‘xx’)
    res.send(‘hi’)
    }))
    app.use((err, req, res, next) => {
    console.error(err)
    res.send(‘error’)
    })
    app.listen(3000)

Leave a Reply