{"id":17035713,"url":"https://github.com/artberri/express-http-context2","last_synced_at":"2025-08-20T02:05:49.258Z","repository":{"id":103992880,"uuid":"608896959","full_name":"artberri/express-http-context2","owner":"artberri","description":"Modern request-scoped storage support for Express.js based on Asynchronous Local Storage.","archived":false,"fork":false,"pushed_at":"2023-04-18T09:58:49.000Z","size":77,"stargazers_count":1,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-19T08:46:41.864Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/artberri.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-03T00:29:44.000Z","updated_at":"2023-03-06T23:21:31.000Z","dependencies_parsed_at":"2023-05-29T19:30:26.320Z","dependency_job_id":null,"html_url":"https://github.com/artberri/express-http-context2","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/artberri/express-http-context2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artberri%2Fexpress-http-context2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artberri%2Fexpress-http-context2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artberri%2Fexpress-http-context2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artberri%2Fexpress-http-context2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/artberri","download_url":"https://codeload.github.com/artberri/express-http-context2/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artberri%2Fexpress-http-context2/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271252993,"owners_count":24726918,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-20T02:00:09.606Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T08:47:51.090Z","updated_at":"2025-08-20T02:05:49.219Z","avatar_url":"https://github.com/artberri.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express HTTP Context 2\n\n[![Rate on Openbase](https://badges.openbase.com/js/rating/express-http-context2.svg)](https://openbase.com/js/express-http-context2?utm_source=embedded\u0026utm_medium=badge\u0026utm_campaign=rate-badge)\n[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.0-4baaaa.svg)](./CODE_OF_CONDUCT.md)\n[![MIT license](https://img.shields.io/npm/l/express-http-context2)](./LICENSE)\n[![Build status](https://github.com/artberri/express-http-context2/actions/workflows/qa.yml/badge.svg)](https://github.com/artberri/express-http-context2/actions/workflows/qa.yml)\n[![codecov](https://codecov.io/gh/artberri/express-http-context2/branch/main/graph/badge.svg?token=VSIC7EAX0Y)](https://codecov.io/gh/artberri/express-http-context2)\n\nModern request-scoped storage support for Express.js, based on native Node.js Asynchronous Local Storage. It's a great place to store user state, claims from a JWT, request/correlation IDs, and any other request-scoped data.\n\nThis package is a drop-in replacement and has been inspired by the work done in [express-http-context](https://github.com/skonves/express-http-context), but with ESM support, no dependencies, and only supporting modern versions of Node.js (+v14).\n\n## Installation\n\n```bash\nnpm install express-http-context2\n# or\nyarn add express-http-context2\n# or\npnpm add express-http-context2\n```\n\n\u003e **Requirements**\n\u003e\n\u003e `express-http-context2` is a middleware intended for `express`, so although it is not explicitly declared as a dependency or peer dependency, it requires `express` to work, as well as `@types/express` if you are using Typescript. The reason the dependency is not explicitly declared is that it could also eventually be used with `fastify` or other Node.js HTTP servers.\n\n## Configuration\n\nUse the middleware immediately before the first middleware that needs to have access to the context. You won't have access to the context in any middleware \"used\" before this one.\n\n```js\nconst express = require('express')\nconst httpContext = require('express-http-context2')\n\nconst app = express()\n// Use any third party middleware that does not need to access the context here, e.g.\n// app.use(some3rdParty.middleware);\napp.use(httpContext.middleware)\n// All code from this point on will have access to the per-request context\n```\n\nNote that some popular middlewares (such as `body-parser`, `express-jwt`) can cause the context to be lost. To work around such problems, it is recommended that you use any third-party middleware that does NOT require the context BEFORE using this middleware.\n\n## Usage\n\nExamples of setting values:\n\n```js\nconst httpContext = require('express-http-context2')\nconst { nanoid } = require('nanoid') // This is just an example, nanoid is not included in this lib\n\napp.use((req, res, next) =\u003e {\n\t// Get the user ID from wherever and save it for later use...\n\thttpContext.set('userId', userId)\n\t// Create a request ID to be able to trace/correlate everything that happens within the same request\n\thttpContext.set('requestId', nanoid())\n})\n```\n\nGet them from anywhere in your code:\n\n```js\nvar httpContext = require('express-http-context2')\n\nfunction logError(error) {\n\tconst userId = httpContext.get('userId')\n\tconst requestId = httpContext.get('requestId')\n\tconsole.error(error, { userId, requestId })\n}\n```\n\n## API\n\n### middleware\n\nIt is an Express.js middleware that is responsible for initializing the independent context for each request. The `get` and `set` calls will operate on a set of keys/values unique to those contexts.\n\n#### Example\n\n```js\nimport { middleware } from 'express-http-context2'\n\napp.use(middleware)\n```\n\n### set\n\nAdds a value to the request context by key.\nIf the key already exists, its value will be overwritten.\nNo value will persist if the context has not yet been initialized.\n\n#### Parameters\n\n- `key` a string key to store the variable by\n- `value` any value to store under the key for the later lookup.\n\n#### Example\n\n```js\nimport { set } from 'express-http-context2'\n\nset('user', { id: 'overwrittenUser', email: 'foo@bar.com' })\n```\n\n### get\n\nGets a value from the request context by key.\nWill return `undefined` if the context has not yet been initialized for this request or if a value is not found for the specified key.\n\n#### Parameters\n\n- `key` a string key to retrieve the stored value for\n\n#### Example\n\n```js\nimport { get } from 'express-http-context2'\n\nconst user = get('user')\n```\n\n## License\n\nExpress HTTP Context 2 is released under the MIT license:\n\nMIT License\n\nCopyright (c) 2023 Alberto Varela Sánchez\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartberri%2Fexpress-http-context2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartberri%2Fexpress-http-context2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartberri%2Fexpress-http-context2/lists"}