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

express-context

v0.7.1

Published

Data encapsulation for express.

Downloads

363

Readme

express-context

Middleware for express to seamlessly contextualize specific properties.

build status coverage license version downloads

It allows for other middleware to be oblivious that properties they are manipulating are encapsulated within a specific context. For example, a property "foo" contextualized on a per-middleware basis means that every time middleware tries to access req.foo it manipulates a variable specific to that piece of middleware. You may then later recover all the different versions of foo, each specific to one particular middleware.

var express = require('express'),
	contextualize = require('express-context'),
	app = express();

function mw1(req, res, next) {
	req.foo = 5;
	req.bar = 10;
	next();
}

function mw2(req, res, next) {
	req.foo = 7;
	req.bar = 1;
	next();
}

var context = contextualize(['foo', 'bar']);

var cmw1 = context.for('a').use(mw1),
	cmw2 = context.for('b').use(mw2);

app.get('/', cmw1, cmw2, function (req, res, next) {

	console.log(cmw1.of(req));
	// { foo: 5, bar: 10 }

	console.log(cmw2.of(req));
	// { foo: 7, bar: 1 }

	next();
});

Building Fluent APIs

Create your API by creating a new context with an ID that's unique to your module, declaring all the properties that your API is concerned with, and then adding in all the mixins your module provides:

var context = require('express-context');

var mixins = {
	ok: function() {
		return this.use(function(req, res, next) {
			req.status(200).send(req.data);
		});
	}
}

function api() {
	return context({
		context: '__my_api',
		properties: [ 'data' ]
	}).mixin(mixins);
}

module.exports = api;

Then make use of your API:

var api = require('api')(),
	app = require('express')();

app.get('/all', api.ok());
app.get('/bob', api.for('my-consumer').use(function(req, res, next) {
	req.data = 'bob';
}).ok());

API

use(method1, method2, ...)

Continue the chain with a new method. This works just like how it does in express.

// res is now middleware that invokes context AND the new given function
var res = context.use(function(req, res, next) {
	next();
});

mixin(properties)

Add new properties to the chain. This does not mutate the chain on which it was called.

// res now has everything in context plus foo = 5
var res = context.mixin({ foo: 5 });

for(middleware)

Get chained middleware representing the contextualized version of the arguments. This does not do anything but set the current context.

// res is a chain whose context is 'abc'
var res = context.for('abc');
// res is a function for the parallel execution of contextualized versions of
// anything used after it in the chain.
var res = context.for(['abc', 'def']);

of(request)

Get the value of the chain's context for a specific request

// res is an object whose keys correspond to all the contexts that were set
// during the processing of req.
var res = context.of(req);
// res is the result of the single context for mw1 that was set during the
// processing of req.
var res = context.for('abc').of(req);