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 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-ez-405

v2.2.2

Published

Simple plug and play function that can be dropped below your route declarations and forgotten about.

Downloads

31

Readme

express-ez-405

Build Passing PRs Welcome License: MIT Release: 2.1

Table of Contents

Introduction

Express Ez 405 provides a quick and dynamic means of generating 405 and 404 errors in Express routes with detailed error messages describing available routes. Unlike other similar solutions this package aims to be set it and forget it, with the error messages being built dynamically from the other methods on the route so that you should never have to update it regardless of how your routes change. Express Ez 405 is lightweight, easy to use, and comes with zero dependencies!

Setup

Install with npm i express-ez-405 and import into the file in which you determine your routing,

const { buildError } = require('express-ez-405');

then drop a catch all route after your last route

app.use("", (req, _, next) => {
  const err = buildError(app, req);
  if (!err) return next();
  return next(err);
});

and then forget about it! You will now be able to add, remove, and update routes and the resulting errors will represent what is currently available, not what was available the last time you touched this.

Outputs

There are three possible outputs of buildError:

  1. null Method used was 'OPTIONS' (prevents handler from catching pre-flights)
  2. Instance of Error with a status of 405 and a message of 'You attempted a GET request to /user/makead try POST.'. Method didn't match any of the avialable methods.
  3. Instance of Error with a status of 404 and a message of 'Could not find the appropriate endpoint for /user/notanendpoint in /user'. No endpoints matched what you were looking for.
  4. Instance of Error with a status of 400 and a message of 'Error with second argument (request). No method property on request.. You passed invalid information. Check the arguments you provided and the order in which they were provided (This will only ever be an issue on initial setup, once everything is there it will be good regardless of how your application grows).

Example

The only thing to watch out for here is to make sure you have an error handler middleware, and that express-easy-405 comes AFTER your last route.

const express = require('express');
const { buildError } = require('express-ez-405');

const app = express();
app.use(express.json());
const userRouter = require('./routes/user');
const mainRouter = require('./routes/main');
const nestedRouter = require('./routes/nested');

// Routes
app.use('/main', mainRouter);
app.use('/user', userRouter);
app.use('/nested/route', nestedRouter);
// Routes

// express-ez-405 handler
app.use('', (req, _, next) => {
  const err = buildError(app, req);
  if (!err) return next();
  return next(err);
});
// express-ez-405 handler

// Error handling
app.use((err, _, res, __) =>
  res.status(err.status).json({ message: err.message })
);
// Error handling

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server listening on port: ${PORT}...`);
});