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

http-handler

v0.0.4

Published

Minimalist handling for http requests. Where non-file urls are handled by index.js in relative folder, and resource files are streamed from resources directory.

Downloads

4

Readme

http-handler

Stability - 2 Unstable

A bare bones http handler for incoming server requests from http.createServer(function (req, res) {});

The handler splits the request into two basic types. Either the request is handled as a file resource, or a module directory (see http://nodejs.org/api/modules.html). The init() function takes an object with the base directories for resources and response modules. These should be different directories to avoid the possiblity of piping a server-side script file to the client. See init() API description below.

The handler first looks for a file resource matching the url path (ignoring any query strings). Then, it looks for a matching module handler.

A response module handler can be an index.js within the pathname of the request url. For example, if the request url is /css/style.css, and there is a file resource located in /css/style.css, the default behavior is to pipe the file resource contents to the response. Or, if there is a directory named /css/style.css/ containing index.js, that directory is loaded as a module resource, and is expected to handle the incoming request. An example index.js module resource might look like this:

If neither a resource exists, nor a module, there is a default /responses/404/index.js where responds with a generic 404 message. The parent module can create a similar 404 module to respond with a specific 404 message, or create a /resources/404 file to respond with a static message.

API

//install
npm install http-handler

//usage
handler = require('http-handler');
handler.init([{
    responses: <directory path to response handlers>,
    resources: <directory path to resources (ex. images, videos, static html)>
}]);

handler.respond(req, res, callback);

/**
 * Initialize the module with two base directory paths identified in config object.
 * config {
 *   responses: <Base directory location for node module handlers>
 *   resources: <Base directory location for static file resources (ie. images, video, css)>
 * }
 */
init: function(config) 

/**
 * Respond to http request 
 * req - http request http.IncomingMessage
 * res - http response http.ServerResponse
 * callback - Callback function(statusCode, err)
 *    statusCode - http status response sent to client
 *    err - Any internal Error encountered such as an exeption thrown in response module
 */
respond: function(req, res, callback)

example:

handler = require('./index').init({
    resources: './parentresources',
    responses: './parentresponses'
});

server = http.createServer(function(req, res) {
    handler.respond(req, res, function (statusCode, err) {
        switch (statusCode) {
            case 200:
                console.log('Completed request successfully: ' + req.url);
                break;
            case 403:
                console.warn('Forbidden: ' + req.url);
                break;
            case 404:
                console.warn('URL not found: ' + req.url);
                break;
            case 500:
                console.warn('500 error: ' + err);
                break;
            default:
                console.error('Unknown status: ' + statusCode);
                console.error('Unknown error: ' + err);
                break;
        }
    });
});