Coder Social home page Coder Social logo

express-cls-hooked's Introduction

travis coveralls npm npm david

Express HTTP Context with async_hooks

This package is forked from express-http-context. It is using cls-hooked which is a fork of continuation-local-storage that uses async_hooks API, so context is preserved even over async/await in node 8+. If you're using node version < 8, just use the original express-http-context.

UPDATE: The original express-http-context was merged from this repo, so for Node >= 8 you can still use the latest version of express-http-context.

Get and set request-scoped context anywhere. This is just an unopinionated, idiomatic ExpressJS implementation of continuation-local-storage. It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data.

How to use it

Install: npm install --save express-cls-hooked

Use the middleware. The earlier the better; you won't have access to the context from any middleware "used" before this one.

var express = require('express');
var httpContext = require('express-cls-hooked');

var app = express();

app.use(httpContext.middleware);
// all code from here on has access to the same context for each request

Set values based on the incomming request:

// Example authorization middleware
app.use((req, res, next) => {
	userService.getUser(req.get('Authorization'), (err, result) => {
		if (err) {
			next(err);
		} else {
			httpContext.set('user', result.user)
			next();
		}
	});
});

Get them from code that doesn't have access to the express req object:

var httpContext = require('express-cls-hooked');

// Somewhere deep in the Todo Service
function createTodoItem(title, content, callback) {
	var user = httpContext.get('user');
	db.insert({ title, content, userId: user.id }, callback);
}

Troubleshooting

To avoid weird behavior with express:

  1. Make sure you require express-cls-hooked in the first row of your app. Some popular packages use async which breaks CLS.
  2. If you are using body-parser and context is getting lost, register it in express before you register express-cls-hooked's middleware.

See Issue #4 for more context. If you find any other weird behaviors, please feel free to open an issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.