{"id":14676830,"url":"https://github.com/arikw/express-response-hooks","last_synced_at":"2025-08-01T10:32:14.495Z","repository":{"id":43187211,"uuid":"511064176","full_name":"arikw/express-response-hooks","owner":"arikw","description":"Extends Express.js with response hooks","archived":false,"fork":false,"pushed_at":"2022-07-07T06:00:26.000Z","size":19,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T03:44:35.706Z","etag":null,"topics":["express","express-js","express-middleware","expressjs"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/express-response-hooks","language":"JavaScript","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/arikw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-06T09:02:51.000Z","updated_at":"2023-01-19T22:25:52.000Z","dependencies_parsed_at":"2022-08-31T15:31:07.648Z","dependency_job_id":null,"html_url":"https://github.com/arikw/express-response-hooks","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arikw%2Fexpress-response-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arikw%2Fexpress-response-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arikw%2Fexpress-response-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arikw%2Fexpress-response-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arikw","download_url":"https://codeload.github.com/arikw/express-response-hooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228364002,"owners_count":17908319,"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","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":["express","express-js","express-middleware","expressjs"],"created_at":"2024-09-12T09:00:57.038Z","updated_at":"2024-12-05T20:12:48.828Z","avatar_url":"https://github.com/arikw.png","language":"JavaScript","funding_links":[],"categories":["Middleware"],"sub_categories":[],"readme":"# Express Response Hooks\n\nExtend [Express.js](https://expressjs.com/) with response hooks, primarily aimed to manipulate the response before being sent to the client.\n\n# Installation\n\n```sh\nnpm install express-response-hooks\n```\n\n# Usage\n\n```js\nconst responseHooks = require('express-response-hooks');\n\n// response hooks initialization\napp.use(responseHooks());\n\n// register a middleware that adds a \"Cache-Control\" header whenever the status code changes\napp.use(function (req, res, next) {\n    // hook on \"statusCode\" changes\n    res.hooks.on('statusCode', ([ statusCode ]) =\u003e {\n        if (req.method.toUpperCase() === 'GET') \u0026\u0026 (statusCode \u003c 400)) {\n            res.set('Cache-Control', 'public, max-age=5, s-maxage=31536000');\n        }\n    });\n);\n```\n\n# API\n\n### require('express-response-hooks')([options])\nCreates a middleware that adds hooks to the response object\n\n#### Options object\n- `plugName`\n  - Type: `string`\n  - Default: `\"hooks\"`\n\n  Controls under which key the hooks event emitter will be available in the response object. Default to `res.hooks`\n\n### res.hooks\nThe hooks [`EventEmitter`](https://nodejs.org/docs/latest/api/events.html#events_class_eventemitter) that enables registration to the following events:\n\n| Event Name   | Trigger          | Arguments Array                             | Trigger |\n|--------------|------------------|---------------------------------------------|-------------|\n| \"statusCode\" | `res.statusCode` | status code (int)                           | `statusCode` property is changed internally |\n| \"set\"        | `res.set()`      | header name (string), header value (string) | `set()` is called internally or explicitly |\n| \"send\"       | `res.send()`     | body (int / object / string)                | `send()` is called internally (e.g., `res.json()` calls it twice) or explicitly |\n\nFor example\n```js\nres.hooks.once('statusCode', ([ statusCode ]) =\u003e {\n  // called once when the res.statusCode is changed for the first time\n});\nres.hooks.on('res', ([ name, value ]) =\u003e {\n  // called whenever res.res() is called internally by express to set default headers or explicitly\n});\n```\n\n#### Data manipulation\n\nChanging the values in the arguments array will change the arguments passed to the original wrapped property/function\n\nFor example\n```js\nres.hooks.on('res', (args) =\u003e {\n  const [ name, value ] = args;\n  if (name === 'cache-control') {\n    // change the value of the header\n    args[1] = args[1].replace('public', 'private');\n  }\n});\n```\n\n### res.hooks.bypass\nAn object with references to the wrapped properties and functions for using them without triggering an event.\n\nFor example\n```js\nres.hooks.on('set', ([ name, value ]) =\u003e {\n  if (name === 'cache-control') {\n    // add additional header without triggering another 'set' event\n    res.hooks.bypass.set('cdn-cache-control', value);\n  }\n});\n```\n\n# Notice\n* You should take into consideration that Express internally calls some of the response functions right before the response gets sent to the client (e.g., adding default headers)\n* `statusCode` manipulation will not work if the hook was triggered by `send()`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farikw%2Fexpress-response-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farikw%2Fexpress-response-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farikw%2Fexpress-response-hooks/lists"}