A dead simple npm library to validate status codes in express.js
MIT License
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?
Checkout this replit example
OR follow the steps below:
pnpm i express-status-validate
or
npm i express-status-validate
or
yarn add express-status-validate
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.listen(3000, () => {
console.log("Server started on port 3000");
});
NOTE: app.use(expressStatusValidate(500)) must be on top to work as desired.
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) :
express-status-validate
is available under the ISC license. See the LICENSE file for more info.
Please read Contributing.md
for details on our code of conduct, and the process for submitting pull requests to us.
npm i -g pnpm
cd package && pnpm i
pnpm run build
Many people have contacted us asking if they can use this code for their own websites. The answer to that question is usually "yes", with attribution. There are some cases, such as using this code for a business or something that is greater than a personal project, that we may be less comfortable saying yes to. If in doubt, please don't hesitate to ask us.
We value keeping this site open source, but as you all know, plagiarism is bad. We spent a non-negligible amount of effort developing, designing, and trying to perfect this iteration of our website, and we are proud of it! All we ask is to not claim this effort as your own.
So, feel free to fork this repo. If you do, please just give us proper credit by linking back to this repo. Refer to this handy quora post if you're not sure what to do. Thanks!