{"id":13754510,"url":"https://github.com/pillarjs/finalhandler","last_synced_at":"2025-06-18T08:35:25.958Z","repository":{"id":17720401,"uuid":"20547377","full_name":"pillarjs/finalhandler","owner":"pillarjs","description":"Node.js final http responder","archived":false,"fork":false,"pushed_at":"2025-06-01T20:26:59.000Z","size":231,"stargazers_count":226,"open_issues_count":13,"forks_count":54,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-06-12T17:05:18.141Z","etag":null,"topics":["404","500","javascript","nodejs"],"latest_commit_sha":null,"homepage":null,"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/pillarjs.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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,"zenodo":null},"funding":{"open_collective":"express"}},"created_at":"2014-06-06T01:27:19.000Z","updated_at":"2025-06-01T20:26:56.000Z","dependencies_parsed_at":"2024-01-13T02:46:36.309Z","dependency_job_id":"6269ea31-60eb-4286-9197-62688a8057e6","html_url":"https://github.com/pillarjs/finalhandler","commit_stats":{"total_commits":342,"total_committers":8,"mean_commits":42.75,"dds":0.04385964912280704,"last_synced_commit":"42a0a2a14ff37fe0bd6413c5986f86fe7a1b2e7e"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"purl":"pkg:github/pillarjs/finalhandler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Ffinalhandler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Ffinalhandler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Ffinalhandler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Ffinalhandler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pillarjs","download_url":"https://codeload.github.com/pillarjs/finalhandler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Ffinalhandler/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260048940,"owners_count":22951180,"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":["404","500","javascript","nodejs"],"created_at":"2024-08-03T10:00:26.101Z","updated_at":"2025-06-18T08:35:20.944Z","avatar_url":"https://github.com/pillarjs.png","language":"JavaScript","readme":"# finalhandler\n\n[![NPM Version][npm-image]][npm-url]\n[![NPM Downloads][downloads-image]][downloads-url]\n[![Node.js Version][node-image]][node-url]\n[![Build Status][github-actions-ci-image]][github-actions-ci-url]\n[![Test Coverage][coveralls-image]][coveralls-url]\n[![OpenSSF Scorecard Badge][ossf-scorecard-badge]][ossf-scorecard-visualizer]\n\nNode.js function to invoke as the final step to respond to HTTP request.\n\n## Installation\n\nThis is a [Node.js](https://nodejs.org/en/) module available through the\n[npm registry](https://www.npmjs.com/). Installation is done using the\n[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):\n\n```sh\n$ npm install finalhandler\n```\n\n## API\n\n```js\nconst finalhandler = require('finalhandler')\n```\n\n### finalhandler(req, res, [options])\n\nReturns function to be invoked as the final step for the given `req` and `res`.\nThis function is to be invoked as `fn(err)`. If `err` is falsy, the handler will\nwrite out a 404 response to the `res`. If it is truthy, an error response will\nbe written out to the `res` or `res` will be terminated if a response has already\nstarted.\n\nWhen an error is written, the following information is added to the response:\n\n  * The `res.statusCode` is set from `err.status` (or `err.statusCode`). If\n    this value is outside the 4xx or 5xx range, it will be set to 500.\n  * The `res.statusMessage` is set according to the status code.\n  * The body will be the HTML of the status code message if `env` is\n    `'production'`, otherwise will be `err.stack`.\n  * Any headers specified in an `err.headers` object.\n\nThe final handler will also unpipe anything from `req` when it is invoked.\n\n#### options.env\n\nBy default, the environment is determined by `NODE_ENV` variable, but it can be\noverridden by this option.\n\n#### options.onerror\n\nProvide a function to be called with the `err` when it exists. Can be used for\nwriting errors to a central location without excessive function generation. Called\nas `onerror(err, req, res)`.\n\n## Examples\n\n### always 404\n\n```js\nconst finalhandler = require('finalhandler')\nconst http = require('http')\n\nconst server = http.createServer((req, res) =\u003e {\n  const done = finalhandler(req, res)\n  done()\n})\n\nserver.listen(3000)\n```\n\n### perform simple action\n\n```js\nconst finalhandler = require('finalhandler')\nconst fs = require('fs')\nconst http = require('http')\n\nconst server = http.createServer((req, res) =\u003e {\n  const done = finalhandler(req, res)\n\n  fs.readFile('index.html', (err, buf) =\u003e {\n    if (err) return done(err)\n    res.setHeader('Content-Type', 'text/html')\n    res.end(buf)\n  })\n})\n\nserver.listen(3000)\n```\n\n### use with middleware-style functions\n\n```js\nconst finalhandler = require('finalhandler')\nconst http = require('http')\nconst serveStatic = require('serve-static')\n\nconst serve = serveStatic('public')\n\nconst server = http.createServer((req, res) =\u003e {\n  const done = finalhandler(req, res)\n  serve(req, res, done)\n})\n\nserver.listen(3000)\n```\n\n### keep log of all errors\n\n```js\nconst finalhandler = require('finalhandler')\nconst fs = require('fs')\nconst http = require('http')\n\nconst server = http.createServer((req, res) =\u003e {\n  const done = finalhandler(req, res, { onerror: logerror })\n\n  fs.readFile('index.html', (err, buf) =\u003e {\n    if (err) return done(err)\n    res.setHeader('Content-Type', 'text/html')\n    res.end(buf)\n  })\n})\n\nserver.listen(3000)\n\nfunction logerror (err) {\n  console.error(err.stack || err.toString())\n}\n```\n\n## License\n\n[MIT](LICENSE)\n\n[npm-image]: https://img.shields.io/npm/v/finalhandler.svg\n[npm-url]: https://npmjs.org/package/finalhandler\n[node-image]: https://img.shields.io/node/v/finalhandler.svg\n[node-url]: https://nodejs.org/en/download\n[coveralls-image]: https://img.shields.io/coveralls/pillarjs/finalhandler.svg\n[coveralls-url]: https://coveralls.io/r/pillarjs/finalhandler?branch=master\n[downloads-image]: https://img.shields.io/npm/dm/finalhandler.svg\n[downloads-url]: https://npmjs.org/package/finalhandler\n[github-actions-ci-image]: https://github.com/pillarjs/finalhandler/actions/workflows/ci.yml/badge.svg\n[github-actions-ci-url]: https://github.com/pillarjs/finalhandler/actions/workflows/ci.yml\n[ossf-scorecard-badge]: https://api.scorecard.dev/projects/github.com/pillarjs/finalhandler/badge\n[ossf-scorecard-visualizer]: https://ossf.github.io/scorecard-visualizer/#/projects/github.com/pillarjs/finalhandler","funding_links":["https://opencollective.com/express"],"categories":["1. 后端开发"],"sub_categories":["1.1 HTTP"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpillarjs%2Ffinalhandler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpillarjs%2Ffinalhandler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpillarjs%2Ffinalhandler/lists"}