Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

http-request-context

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-request-context

Store context in http middleware lifecycle.

  • 0.2.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
13
decreased by-65.79%
Maintainers
1
Weekly downloads
 
Created
Source

HTTP Request Context

npm package

Build Status Coverage Status Known Vulnerabilities FOSSA Status npm package

NPM downloads Dependency Status Dependency Status Standard - JavaScript Style Guide

Set and get request-scoped context anywhere.

Requirement

Nodejs version >= 8.2.0

This module uses the newer async_hooks API which is considered Experimental by Nodejs.

Parameter

Parameter configuration must be require before, but normally no config are required.

NameDescriptionDefault
process.env.HTTP_REQUEST_CONTEXT_INTERVALremove expired callstack interval(ms)10000
process.env.HTTP_REQUEST_CONTEXT_EXPIREcallstack expire time(ms)150000

Middleware

  • httpRequestContext.middleware Express middleware.
  • httpRequestContext.koaMiddleware Koa middleware.

Set Context

  • httpRequestContext.set(key, value) Set context anywhere.
  • httpRequestContext.set({ key: value }) This is also OK.

Get Context

  • httpRequestContext.get(key) Get the [key] attribute of the context.
  • httpRequestContext.get() Gets an object containing all context properties.

How to Use

see example here.

Install

npm install http-request-context --save

Express

Init
const httpRequestContext = require('http-request-context')

app.use(httpRequestContext.middleware)
Set Context
const httpRequestContext = require('http-request-context')

// set context by key-value
app.use((req, res, next) => {
  setTimeout(() => {
    httpRequestContext.set('foo', 'bar')
    next()
  }, 100)
})
Get Context
const httpRequestContext = require('http-request-context')

httpRequestContext.get('foo') // 'bar'

Koa

Init
const httpRequestContext = require('http-request-context')

app.use(httpRequestContext.koaMiddleware)
Set Context
const httpRequestContext = require('http-request-context')

// set context by key-value
app.use(async (ctx, next) => {
  await new Promise(resolve => {
    setTimeout(() => {
      httpRequestContext.set('user', 'user')
      resolve()
    }, 300)
  })
  await next()
})
Get Context
const httpRequestContext = require('http-request-context')

httpRequestContext.get('foo') // 'bar'

Tips

MySQL

If you init mysql connect before http server start, you may get context undefined in mysql query callback scope.

googleapis/cloud-trace-nodejs #946

nodejs/node #22360

mysqlConnection.query('SELECT * FROM table', (error, results, fields) => {
  httpRequestContext.get('foo') // undefined
})

You can use util.promisify to avoid it.

util.promisify(mysqlConnection.query).bind(mysqlConnection)('SELECT * FROM table')
  .then((results, fields) => {
    httpRequestContext.get('foo') // 'bar'
  })
  .catch(error => {})

Keywords

FAQs

Package last updated on 09 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc