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

http-request-handler

v2.0.1

Published

A tiny library for parsing and handling HTTP requests.

Readme

http-request-handler

http-request-handler is a tiny library for parsing and handling HTTP-requests in Node.js.

Installation

npm install http-request-handler

Usage

Basics

Create a new HTTPRequestHandler object, declare your routes and then use the getListener() function to start the server:

const {HTTPRequestHandler, HttpMethod} = require("http-request-handler");
const http = require("http");

let handler = new HTTPRequestHandler({
    endpoint: "/myapi"
});

handler.on(HttpMethod.HTTP_GET, "/hello", [], (request, response) => {
    response.send("world");
});

http.createServer(handler.getListener()).listen(3000);

// visit http://localhost:3000/myapi/hello

Middleware

Middleware functions can preprocess requests (for example by attaching custom data) before they are passed on to the route handler. They can also reject or respond to requests. The middleware functions of a route will be executed in the order they are listed until a middleware function sends a response, in which case neither the remaining middleware functions or the route handler will be called.


// This middleware function rejects all requests that don't have a valid "token" cookie.
const isUserLoggedIn = (request, response, data, resolve, reject) => {
    let valid = checkCookie(request.cookies.token);

    // allow this request to be passed to the next middleware function
    if (valid) resolve(); 
    else reject(403); // 403 Forbidden
};

// This middleware function assumes that the "token" cookie is valid. It will attach a "userID" property to the data object containing the ID of the logged in user.
const getUserID = (request, response, data, resolve, reject) => {
    let userID = getUserID(request.cookies.token);
    resolve({
        userID: userID
    });
};

handler.on(HttpMethod.HTTP_POST, "/upload", [isUserLoggedIn, getUserID], (request, response, data) => {
    // => this request comes from a logged in user with the ID data.userID
});

HTTPS

Since http-request-handler isn't a server, you can setup HTTPS the default way:

const {HTTPRequestHandler, HttpMethod} = require("http-request-handler");
const http = require("http");
const https = require("https");
const fs = require("fs");

const httpsOptions = {
    key: fs.readFileSync("keyFile.pem"),
    cert: fs.readFileSync("certFile.pem")
};

let handler = new HTTPRequestHandler({
    endpoint: "/myapi"
});

handler.on(HttpMethod.HTTP_GET, "/hello", [], (request, response) => {
    response.send("world");
});

https.createServer(httpsOptions, handler.getListener()).listen(3443);

// use HTTPRequestHandler.redirectToHTTPS to redirect all traffic to HTTPS
http.createServer(handler.redirectToHTTPS()).listen(3000);

Development

  • to run the tests run npm test
  • to compile TypeScript to JavaScript run npm run prepublish