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-status-validate

v2.1.1

Published

A dead simple npm library to validate express.js response status codes.

Downloads

52

Readme

Inspiration for this package!

  1. To learn how to make packages 😉
  2. I spent around 40-50 minutes debugging why SRMKZILLA's backend kept crashing. Found out someone passed the mongo error codes(single digit) to express and viola.
  3. Bun v1 came out and I decided to ditch pnpm and node.

What does it do?

It does what a middleware should, it sits quietly and on a day to day basis, you never notice it. Express is great, but it can be better. In TypeScript, passing a wrong status code is not that easy. But in JavaScript? One could pass a tank and the code would run (Thanks JS :) ). This package safeguards your app from throwing unwanted errors or crashing. What can you pass?

  1. If you pass a valid status code like 200, it works. It doesn't change anything in the original status code.
  2. BUT, if you pass "200" or "200OK", it converts it to 200 and sends it. But what if you pass something like 799 (invalid status code)? In that case a default status code is set and sent.
  3. And you guesses correct, you can easily set the default status code you want.

How to use it in your JS/TS project?

  1. CD into the root folder of your project!
  2. Install express-status-validate using bun (bun is cool, yarn is boring and npm is npm.) Pnpm is also cool btw :p
bun add express-status-validate

or

pnpm i express-status-validate

or

npm i express-status-validate

or

yarn add express-status-validate
  1. Open the entry point of your express app.

Our express application before adding the package:

const express = require("express");

const app = express();

app.get("/healthcheck", (req, res) => {
  //This should throw an error and crash.
  res.status(200123).send("OK");
});

app.listen(3000, () => {
  console.log("Server started on port 3000");
});

After adding the express-status-validate :

const express = require("express");
const expressStatusValidate = require("express-status-validate");

const app = express();

// 500 is optional, you can pass any valid http status code
app.use(expressStatusValidate(500));

app.get("/healthcheck", (req, res) => {
  //This should throw an error and crash an application but it doesn't anymore.
  res.status(200123).send("OK");
});

app.post("/healthcheck", (req, res) => {
  //This should throw an error and crash an application but it doesn't anymore.
  res.status("200abs").send("OK");
});

app.listen(3000, () => {
  console.log("Server started on port 3000");
});

Checkout this replit example

NOTE: app.use(expressStatusValidate(500)) must be on top to work as desired.

Examples:

We are in the process of adding more examples and testing this library to make sure it works smoothly everywhere. Currently we have the following examples (Shoutout to Harshit Singh, my roommate :p for being the first user of this library) :

  1. JavaScript Example
  2. TypeScript Example

License 📜

express-status-validate is available under the ISC license. See the LICENSE file for more info.

Contributing 🤝

Please read Contributing.md for details on our code of conduct, and the process for submitting pull requests to us.

How to contribute to this project:

  1. Fork it 😜 and make sure you have Node(>14) installed!
  2. Install the pnpm using
npm i -g pnpm
  1. Install the dependencies
cd package && pnpm i
  1. Do you magic (We recommend that you stick to the code and not configuration files unless there is a specific reason to do so)🪄
  2. Build the TypeScript
pnpm run build
  1. Run npm link and move to the examples and run npm link and test it! Refer to this!
  2. Make sure you run all the tests before making a pull request!
  3. Make a pull request, sit back and enjoy while we review your changes.

Example without express-status-validate:

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: { a: 's' }
    at new NodeError (node:internal/errors:399:5)
    at ServerResponse.writeHead (node:_http_server:344:11)
    at ServerResponse._implicitHeader (node:_http_server:335:8)
    at write_ (node:_http_outgoing:908:9)
    at ServerResponse.end (node:_http_outgoing:1016:5)

Example with express-status-validate enabled: