{"id":21944235,"url":"https://github.com/michaelkrone/request-context","last_synced_at":"2025-08-21T18:33:19.796Z","repository":{"id":28658170,"uuid":"32177645","full_name":"michaelkrone/request-context","owner":"michaelkrone","description":"Simple connect middleware for accessing data in a request context.","archived":false,"fork":false,"pushed_at":"2017-07-07T14:45:27.000Z","size":54,"stargazers_count":57,"open_issues_count":2,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-11T15:30:50.314Z","etag":null,"topics":["javascript","middleware"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/michaelkrone.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-13T19:54:08.000Z","updated_at":"2022-10-05T17:01:35.000Z","dependencies_parsed_at":"2022-09-05T11:20:53.760Z","dependency_job_id":null,"html_url":"https://github.com/michaelkrone/request-context","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/michaelkrone/request-context","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelkrone%2Frequest-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelkrone%2Frequest-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelkrone%2Frequest-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelkrone%2Frequest-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/michaelkrone","download_url":"https://codeload.github.com/michaelkrone/request-context/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelkrone%2Frequest-context/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270637919,"owners_count":24620430,"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-15T02:00:12.559Z","response_time":110,"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":["javascript","middleware"],"created_at":"2024-11-29T04:15:11.522Z","updated_at":"2025-08-21T18:33:19.449Z","avatar_url":"https://github.com/michaelkrone.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# request-context\nSimple connect middleware for accessing data in a request context.\nWrap the request handling in a domain and set and access data for the current request lifecycle only.\nAll following functions will be run in the created 'namespace'.\n\n**See the [Domain Docs](https://nodejs.org/api/domain.html) for further information on error handling\nfor domains. Note that the domain module is pending deprecation!**\n\n## The problem\nYou would like to access data from the request or any middleware in a completely different context.\nDue to the async architecture of Node it can become a nightmare to pass the data to all callbacks\nis the function chain. This module provides a middleware and an easy to use API to access data\nfrom anywhere in the function chain. No matter if the functions are called async or not.\n\n## Install\n\n```sh\n$ npm install request-context\n```\n\n## Example\n\nserver config in app.js:\n```js\nconst app = express();\nconst contextService = require('request-context');\n\n// wrap requests in the 'request' namespace (can be any string)\napp.use(contextService.middleware('request'));\n\n// set the logged in user in some auth middleware\napp.use(function (req, res, next) {\n\tUser.findById(req.cookies._id, (err, user) =\u003e {\n\t\t// set the user who made this request on the context\n\t\tcontextService.set('request:user', user);\n\t\tnext();\n\t});\n});\n\n// save a model in put requests\napp.put(function (req, res, next) {\n\tnew Model(req.body).save((err, doc) =\u003e res.json(doc));\n});\n\n// always use an default express/connect error handling middleware\n// it will be called if any errors occur in the domain\n// see http://expressjs.com/en/guide/error-handling.html\napp.use(function (err, req, res, next) {\n\tres.status(err.status || 500);\n});\n\n// start server etc.\n[...]\n```\n\nIn the Model definition file:\n```js\nvar contextService = require('request-context');\n\n[...]\n\n// set the user who made changes to this document\n// note that this method is called async in the document context\nmodelSchema.pre('save', function (next) {\n\t// access the user object which has been set in the request middleware\n\tthis.modifiedBy = contextService.get('request:user.name');\n\t// or this.modifiedBy = contextService.get('request').user.name;\n\tnext();\n});\n```\n\n## API\n\nAlso available on [the github pages](http://michaelkrone.github.io/request-context/).\n\n- `middleware`\nReturns 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.\n```js\nvar middleware = require('request-context').middleware;\napp.use(middleware('some namespace'));\n```\n\n- `set`, `setContext`\nSet the context for a key on the context created by the middleware.\n```js\nvar contextService = require('request-context');\ncontextService.set('namespace:key', {some: 'value'});\ncontextService.set('namespace:key.some', 'other');\n```\n\n- `get`, `getContext`\nGet the context for a key on the context created by the middleware.\n```js\nvar contextService = require('request-context');\ncontextService.get('namespace:key.some'); // returns 'other'\n```\n\n## Object Path Syntax\nAny value from the context object can be accessed by a simple object dot notation:\n\n```js\nvar contextService = require('request-context');\n\n// set an object on the namespace\ncontextService.set('namespace:character',\t{\n\t\tname: 'Arya Stark',\n\t\tlocation: {\n\t\t\tname: 'Winterfell',\n\t\t\tregion: 'North'\n\t\t}\n\t});\n\n// this will return the complete object\nvar char = contextService.get('namespace:character');\n\n// work with the object\nvar region = char.location.region;\n\n// this will return 'Arya Stark'\ncontextService.get('namespace:character.name');\n\n// this will set the region to 'Westeros'\ncontextService.set('namespace:character.location.region', 'Westeros');\n\n```\n\n## Documentation\n\nThe documentation is available on [the github pages](http://michaelkrone.github.io/request-context/).\nTo generate the jsdoc documentation run\n```bash\n$ gulp docs\n```\n\n## Test\n\nTo run the packaged tests:\n```bash\n$ gulp test\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaelkrone%2Frequest-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichaelkrone%2Frequest-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaelkrone%2Frequest-context/lists"}