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:

Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 ⋅ 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 ⋅ 6 min read
Exploring Zed, A Newly Open Source Code Editor Written In Rust

Exploring Zed, an open source code editor written in Rust

The Zed code editor sets itself apart with its lightning-fast performance and cutting-edge collaborative features.

Nefe Emadamerho-Atori
Apr 22, 2024 ⋅ 7 min read
Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries — Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 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