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:

Exploring The Aha Stack: Astro, Htmx, Alpine — A Complete Tutorial With A Demo Project And Comparison To Other Stacks

Exploring the AHA stack: Tutorial, demo, and comparison

The AHA stack — Astro, htmx, and Alpine — is a solid web development stack for smaller apps that emphasize frontend speed and SEO.

Oyinkansola Awosan
May 3, 2024 ⋅ 13 min read
Comparing Hattip Vs Express Js For Modern Application Development

Comparing Hattip vs. Express.js for modern app development

Explore what Hattip is, how it works, its benefits and key features, and the differences between Hattip and Express.js.

Antonello Zanini
May 2, 2024 ⋅ 8 min read
Using React Shepherd To Build A Site Tour

Using React Shepherd to build a site tour

React Shepherd stands out as a site tour library due to its elegant UI and out-of-the-box, easy-to-use React Context implementation.

Onuorah Bonaventure
May 1, 2024 ⋅ 14 min read
A Guide To Cookies In Next Js

A guide to cookies in Next.js

Cookies are crucial to web development. This article will explore how to handle cookies in your Next.js applications.

Georgey V B
Apr 30, 2024 ⋅ 10 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