All Projects → michaelkrone → request-context

michaelkrone / request-context

Licence: other
Simple connect middleware for accessing data in a request context.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to request-context

Compression
Node.js compression middleware
Stars: ✭ 2,506 (+4456.36%)
Mutual labels:  middleware
Router.cr
Minimum High Performance Middleware for Crystal Web Server.
Stars: ✭ 231 (+320%)
Mutual labels:  middleware
Golf
⛳️ The Golf web framework
Stars: ✭ 248 (+350.91%)
Mutual labels:  middleware
Toa
A pithy and powerful web framework.
Stars: ✭ 220 (+300%)
Mutual labels:  middleware
Mux.jl
Middleware for Julia
Stars: ✭ 225 (+309.09%)
Mutual labels:  middleware
Redux Websocket
A Redux middleware to handle WebSocket connections.
Stars: ✭ 232 (+321.82%)
Mutual labels:  middleware
Iceoryx
iceoryx - true zero-copy inter-process-communication
Stars: ✭ 208 (+278.18%)
Mutual labels:  middleware
Go Gin Prometheus
Gin Web Framework Prometheus metrics exporter
Stars: ✭ 248 (+350.91%)
Mutual labels:  middleware
Mux
A powerful HTTP router and URL matcher for building Go web servers with 🦍
Stars: ✭ 15,667 (+28385.45%)
Mutual labels:  middleware
Clevergo
👅 CleverGo is a lightweight, feature rich and high performance HTTP router for Go.
Stars: ✭ 246 (+347.27%)
Mutual labels:  middleware
Caddy Authz
Caddy-authz is a middleware for Caddy that blocks or allows requests based on access control policies.
Stars: ✭ 221 (+301.82%)
Mutual labels:  middleware
Letsencrypt Rails Heroku
Automatic LetsEncrypt SSL certificates in your Rails app on Heroku.
Stars: ✭ 223 (+305.45%)
Mutual labels:  middleware
My Review
主要存放平时理论学习,比如java jdk源码分析、并发理论;面试、数据库、Linux、中间件、分布式、网络协议等方向
Stars: ✭ 237 (+330.91%)
Mutual labels:  middleware
Goku Api Gateway
A Powerful HTTP API Gateway in pure golang!Goku API Gateway (中文名:悟空 API 网关)是一个基于 Golang开发的微服务网关,能够实现高性能 HTTP API 转发、服务编排、多租户管理、API 访问权限控制等目的,拥有强大的自定义插件系统可以自行扩展,并且提供友好的图形化配置界面,能够快速帮助企业进行 API 服务治理、提高 API 服务的稳定性和安全性。
Stars: ✭ 2,773 (+4941.82%)
Mutual labels:  middleware
Csaguzzlebundle
A bundle integrating Guzzle >=4.0 in Symfony
Stars: ✭ 248 (+350.91%)
Mutual labels:  middleware
Laravel Multisite
Multiple sites on one codebase
Stars: ✭ 214 (+289.09%)
Mutual labels:  middleware
Chromelogger Python
Python library for logging variables to Google Chrome console
Stars: ✭ 232 (+321.82%)
Mutual labels:  middleware
Imagesharp.web
🌐 High Performance Image Processing Middleware for ASP.NET- Core.
Stars: ✭ 250 (+354.55%)
Mutual labels:  middleware
Node Sass Middleware
connect middleware extracted from node-sass
Stars: ✭ 247 (+349.09%)
Mutual labels:  middleware
Express Basic Auth
Plug & play basic auth middleware for express
Stars: ✭ 241 (+338.18%)
Mutual labels:  middleware

request-context

Simple connect middleware for accessing data in a request context. Wrap the request handling in a domain and set and access data for the current request lifecycle only. All following functions will be run in the created 'namespace'.

See the Domain Docs for further information on error handling for domains. Note that the domain module is pending deprecation!

The problem

You would like to access data from the request or any middleware in a completely different context. Due to the async architecture of Node it can become a nightmare to pass the data to all callbacks is the function chain. This module provides a middleware and an easy to use API to access data from anywhere in the function chain. No matter if the functions are called async or not.

Install

$ npm install request-context

Example

server config in app.js:

const app = express();
const contextService = require('request-context');

// wrap requests in the 'request' namespace (can be any string)
app.use(contextService.middleware('request'));

// set the logged in user in some auth middleware
app.use(function (req, res, next) {
	User.findById(req.cookies._id, (err, user) => {
		// set the user who made this request on the context
		contextService.set('request:user', user);
		next();
	});
});

// save a model in put requests
app.put(function (req, res, next) {
	new Model(req.body).save((err, doc) => res.json(doc));
});

// always use an default express/connect error handling middleware
// it will be called if any errors occur in the domain
// see http://expressjs.com/en/guide/error-handling.html
app.use(function (err, req, res, next) {
	res.status(err.status || 500);
});

// start server etc.
[...]

In the Model definition file:

var contextService = require('request-context');

[...]

// set the user who made changes to this document
// note that this method is called async in the document context
modelSchema.pre('save', function (next) {
	// access the user object which has been set in the request middleware
	this.modifiedBy = contextService.get('request:user.name');
	// or this.modifiedBy = contextService.get('request').user.name;
	next();
});

API

Also available on the github pages.

  • middleware Returns a function that can be used as connect middleware. Takes a string as the name of the namespace as its argument. All functions called after this middleware, async or not, will have read/write access to the context.
var middleware = require('request-context').middleware;
app.use(middleware('some namespace'));
  • set, setContext Set the context for a key on the context created by the middleware.
var contextService = require('request-context');
contextService.set('namespace:key', {some: 'value'});
contextService.set('namespace:key.some', 'other');
  • get, getContext Get the context for a key on the context created by the middleware.
var contextService = require('request-context');
contextService.get('namespace:key.some'); // returns 'other'

Object Path Syntax

Any value from the context object can be accessed by a simple object dot notation:

var contextService = require('request-context');

// set an object on the namespace
contextService.set('namespace:character',	{
		name: 'Arya Stark',
		location: {
			name: 'Winterfell',
			region: 'North'
		}
	});

// this will return the complete object
var char = contextService.get('namespace:character');

// work with the object
var region = char.location.region;

// this will return 'Arya Stark'
contextService.get('namespace:character.name');

// this will set the region to 'Westeros'
contextService.set('namespace:character.location.region', 'Westeros');

Documentation

The documentation is available on the github pages. To generate the jsdoc documentation run

$ gulp docs

Test

To run the packaged tests:

$ gulp test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].