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

contextualize

v0.1.1

Published

A simple tool for lazily bootstrapping any nodejs app into each application context.

Downloads

3

Readme

contextualize

A simple tool for lazily bootstrapping any nodejs app into each application context.

Basic Usage

contextualize creates an http server for you and provides a mechanism for defining the context for each request, and a mechanism for bootstrapping an application for each unique context.

var contextualize = require('contextualize');

contextualize()
    .context(function(request, done) {
        // Set the context for this request
        done(null, { foo:'bar' });
    })
    .bootstrap(function(context, done) {
        // Bootstrap an application for a context
        done(null, function(req, res) {
            res.end('Hello World');
        });
    })
    .listen(80);

Practical Use

Here is an example of how you might switch contexts based on environment and locale code in the host name:

contextualize()
    .context(function(request, done) {
        // Extract the environment and locale code from the host name (e.g. prod.en-US.foobar.com)
        var m = request.host.match(/^(\w+)\.([a-z]{2}-[A-Z]{2})\./);
        
        // Return the context
        done(null, {
            environment: m[1],
            locale: m[2]
        });
    })
    .bootstrap(function(context, done) {
        // Bootstrap an application for each unique context
        done(null, new Application(context));
    })
    .listen(80);

Preloading Contexts at Startup

By default the first request in a context will take the hit of bootstrapping the application for that context. If you want to preload known contexts to avoid that latency on the first request then you can use the preload method:

contextualize()
    .context(getContext)
    .bootstrap(bootstrapApp)
    .preload([{
        locale: 'en-US',
        environment: 'prod'
    }, {
        locale: 'pt-BR',
        environment: 'prod'
    }])
    .listen(80);

Pre-Context Switch Middleware

You may want to apply some middleware (e.g. static file serving, cookie parsing, body parsing, user authentication, etc.) prior to setting the context for a request.

contextualize()
    // Attach middleware here
    .use(staticMiddleware('./public', './static'))
    .use(authMiddleware())
    .context(function(request, done) {
        // Use result of authentication middleware in the context
        done(null, {
            authorized: request.authorized,
        });
    })
    .bootstrap(function(context, done) {
        // Bootstrap seperate apps for logged in or not logged in users
        done(null, context.authorized ? new PublicApplication() : new PrivateApplication());
    })
    .listen(80);

Applications

Applications returned by your bootstrap function are either a Function or an Object with a handleRequest method.

contextualizer()
    .context(getContext)
    .bootstrap(function(context, done) {
        done(null, function(req, res) {
            res.end('Hello World');
        });
    });
    .listen(80);

or:

contextualizer()
    .context(getContext)
    .bootstrap(function(context, done) {
        done(null, {
            handleRequest: function(req, res) {
                res.end('Hello World');
            }
        });
    })
    .listen(80);

Note that express and connect applications are already functions with this interface so this will work as well:

contextualizer()
    .context(getContext)
    .bootstrap(function(context, done) {
        var app = require('express')();

        app.get('/sup', function(req, res){
            res.send(200, 'Hello World');
        });

        done(null, app);
    })
    .listen(80);