2024-04-26
2960
#node
Standard Liv
3694
Apr 26, 2024 ⋅ 10 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:

Rust logo over black marble background.

Handling memory leaks in Rust

Learn how to manage memory leaks in Rust, avoid unsafe behavior, and use tools like weak references to ensure efficient programs.

Ukeje Goodness
Nov 20, 2024 ⋅ 4 min read
Robot pretending to be a person.

Using curl-impersonate in Node.js to avoid blocks

Bypass anti-bot measures in Node.js with curl-impersonate. Learn how it mimics browsers to overcome bot detection for web scraping.

Antonello Zanini
Nov 20, 2024 ⋅ 13 min read
Solving Eventual Consistency In Frontend

Solving eventual consistency in frontend

Handle frontend data discrepancies with eventual consistency using WebSockets, Docker Compose, and practical code examples.

Kayode Adeniyi
Nov 19, 2024 ⋅ 6 min read
How To Use Lazy Initialization Pattern With Rust 1.80

How to use the lazy initialization pattern with Rust 1.80

Efficient initializing is crucial to smooth-running websites. One way to optimize that process is through lazy initialization in Rust 1.80.

Yashodhan Joshi
Nov 18, 2024 ⋅ 5 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