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:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 ⋅ 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 ⋅ 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 ⋅ 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 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