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

@nicolasparada/httptools

v0.9.0

Published

HTTP Tools

Downloads

7

Readme

@nicolasparada/httptools

This package provides some utilities to complement Node's HTTP server.

Shipped like an ES module

Routing

import { createServer } from 'http'
import { createRouter } from '@nicolasparada/httptools'

const router = createRouter()
router.handle('GET', '/', (req, res) => {
    res.end('Hello there 🙂')
})

const server = createServer(router.handler)
server.listen(3000, '127.0.0.1', () => {
    console.log('Server running at http://localhost:3000 🚀')
})

You can register HTTP handlers for a given HTTP verb and URL pattern.

Pattern Matching and Context

import { contextFor, pattern } from '@nicolasparada/httptools'

router.handle('GET', pattern`/hello/{name}`, (req, res) => {
    const ctx = contextFor(req)
    const params = ctx.get('params')
    res.end(`Hello, ${params.name}!`)
})

You can create dynamic routes by passing a regular expression. pattern() is a tagged template literal function that converts the given pattern into a regular expression for simplicity. In this example, it's equivalent to /^\/hello\/(?<name>[^\/]+)$/.

You can capture parameters from the URL with a curly braces syntax as shown there. You can also use a wilcard * to capture anything.

Inside the request context, you'll find a "params" object with all the URL parameters. Context can be filled with your own data. See middleware down below. I do that to not mess with the Node.js API.

Middleware

router.handle('GET', '/auth_user', withAuthUser(authUserHandler))

function withAuthUser(next) {
    return (req, res) => {
        const token = extractToken(req)
        const authUser = decodeToken(token)
        const ctx = contextFor(req)
        ctx.set('auth_user', authUser)
        return next(req, res)
    }
}

function authUserHandler(req, res) {
    const ctx = contextFor(req)
    const authUser = ctx.get('auth_user')
    res.setHeader('Content-Type', 'application/json; charset=utf-8')
    res.end(JSON.stringify(authUser))
}

contextFor() will give you a WeakMap in which you can save data scoped to the request. Just use function composition for middleware.

Sub-routing

import { createRouter, pattern, stripPrefix } from '@nicolasparada/httptools'

const api = createRouter()
api.handle('GET', '/', handler)

const router = createRouter()
router.handle('*', pattern`/api/*`, stripPrefix('/api', api.handler))

stripPrefix() is a middleware that trims the given prefix from the request URL. That way, you can compose multiple routers.