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

light-http-router

v1.0.3

Published

A lightweight HTTP router

Downloads

3

Readme

Http router

A lightweight HTTP router for NodeJS with a very small API.

API

    var http = require('http');
    var app = require('light-http-router'); // entry point for handling HTTP requests to the server

    var router = app.Router();
    router.handle('/people', 'get', function(req, res){
        return res.end('We are serving people');
    })

    app.Use(router);

    http.createServer(app.Serve).listen(80);

Package methods and API

  • Router: a function which returns an object with a handle function and a collection of handlers on the Router. You can have multiple Routers and each of them have their separate handlers objects that are finally aggregated by the app

  • Use: Accepts a router as its only parameter and copies all the prospective request handlers into the app general handler

  • Serve: The entry point function that takes the server IncomingMessage object and returns a ServerResponse. This function is passed in to the http.createServer function

Usage

const routerApp = require('router');

.Router()

This is the main router of the app that creates http request handles and matches it to their corresponding url paths. It returns an object of handlers and a handle property that's a function. It is used as follows:

    const router = routerApp.Router();
    router.handle('/poeple', 'get', function(req, res){
        return res.end("How are you?");
    })

The handle method of the router object takes the url path, url method and request handler as arguments and maps them accordingly in the handlers collection.

Supported url patterns

Light HTTP router supports different types of url paths such as plain string urls, regular expression paths and paths with url parameters. Examples are as follows:

    router.handle('/poeple', 'get', function(req, res){ // plain string path
        return res.end("How are you?");
    })

    router.handle(/name.*?/, 'get', function(req, res){ // regular expression path
        return res.end("How are you?");
    })

    router.handle('/people/:personId', 'get', function(req, res){ // path with url parameters. this adds a params property into the request (http.incomingMessage) object
        console.log("request parameter is", req.params.personId)
        return res.end("How are you?");
    })

    router.handle('/people/:personId/orders/:orderId', 'get', function(req, res){ // path with url parameters. this adds a params property into the request (http.incomingMessage) object
        console.log("request parameter is personId", req.params.personId)
        console.log("request parameter is orderId", req.params.orderId)
        return res.end("How are you?");
    })

It also extracts query parameters from the url path and creates a query object property on the request (http.incomingMessage) object.

.Use(router)

Its only argument is a router object containing all the handlers that have been assigned to specific url paths. It mounts the router handlers into the app so that the app can now be aware of all the routes created across files in different routers. It can be used as follows:

    routerApp.use(router);

.Serve(req, res)

This method is passed into and called by the created http server whenever a request is made to the server. It is the main entry point of the router and responds accordingly to requests that have been mapped to specific handlers.

    const http = require('http');

    http.createServer(routerApp.Serve).listen(80);

.Static(staticFilesDirectory)

This is a method to mount the location for serving static files. If you intend serving static files from your server, this is the function to call. It is used as thus:

    
    app.Use(app.Static('public'))

Todo

  • Create functionality for setting status codes