npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lgdweb/common-express-helpers

v2.0.5

Published

Reusable helpers for express server projects

Readme

Common Express Helpers

Reusable helpers and middlewares for express server projects.

Connect Mongo Db

Helper function to connect to a Mongo Db database using Mongoose.

Parameters

Object of parameters to pass to the function

  • mongoOptions Type: Object - Options to pass to Mongoose

  • mongoUri Type: String - Mongo Db uri to connect

  • verbose Type: Boolean - Print the Mongo Db Uri in the console, do not use this in production

Usage

import { connectMongoDb } from '@lgdweb/common-express-helpers'

const mongoOptions = {
  useNewUrlParser: true,
  useUnifiedTopology: true
}
const mongoUri = 'mongodb://localhost:27017/dbName'
const verbose = process.env.NODE_ENVIRONMENT === 'development'

connectMongoDb({ mongoOptions, mongoUri, verbose })

Setup Close On Exit

Helper function that closes properly an Express server.

Parameters

An instance of Express.

Usage

import { setupCloseOnExit } from '@lgdweb/common-express-helpers'
import express from 'express'

const app = express()
const port = process.env.PORT || 5000

const server = app.listen(port, '0.0.0.0', () =>
  console.info(`Server is listening on http://localhost:${port}`)
)

setupCloseOnExit(server)

Get Details For Error

Helper function that returns formated http error details based on some error types and using http-errors package.

Parameters

A standard Error object.

Usage

This helper function is used internally in the errorHandler middleware

Error Handler Middleware

Middleware for handling errors from Express app that returns a formated error with some details base on the error type.

Parameters

Standard parameters for Express middlewares:

  • error Type: Error - Standard Error or HttpError instance for nice formated error details
  • req Type: Request - The request object from Express
  • res Type: Response - The Response object from Express
  • next Type: Next - The Next object from Express

Usage

import { errorHandler } from '@lgdweb/common-express-helpers'
import express from 'express'

const app = express()

app.use(errorHandler)

const port = process.env.PORT || 5000

app.listen(port, '0.0.0.0', () =>
    console.info(`Server is listening on http://localhost:${port}`)

Request Validator Middleware

Middleware that validates a request using Express Validator and returns well formatted validation errors or continue if no error.

Parameters

Validation rules same as express-validator package.

Usage

import { errorHandler, requestValidator } from '@lgdweb/common-express-helpers'
import express from 'express'
import { body } from 'express-validator'

const app = express()

app.use(express.json())

const emailField = [
  body('email').exists().withMessage('Email field is required'),
  body('email')
    .notEmpty()
    .withMessage('Email must not be empty')
    .bail()
    .isEmail()
    .withMessage('Email must be a valid email')
    .optional()
    .normalizeEmail()
]

app.post('/api/users', requestValidator(emailField), async (req, res) => {
  const { email } = req.body

  res.status(201).json({ message: `User created with email: ${email}` })
})

app.use(errorHandler)

const port = process.env.PORT || 5000

app.listen(port, '0.0.0.0', () => console.info(`Server is listening on http://localhost:${port}`))

Not Found Middleware

Middleware that handles all routes that not match and returns a 404 NotFound error.

Parameters

Standard Express middleware parameters.

Usage

import { errorHandler, notFound } from '@lgdweb/common-express-helpers'
import express from 'express'

const app = express()

app.all('*', notFound)
app.use(errorHandler)

const port = process.env.PORT || 5000

app.listen(port, '0.0.0.0', () => console.info(`Server is listening on http://localhost:${port}`))