opusdev/vector-similarity-api
1
1# finalhandler2 3[![NPM Version][npm-image]][npm-url]4[![NPM Downloads][downloads-image]][downloads-url]5[![Node.js Version][node-image]][node-url]6[![Build Status][github-actions-ci-image]][github-actions-ci-url]7[![Test Coverage][coveralls-image]][coveralls-url]8 9Node.js function to invoke as the final step to respond to HTTP request.10 11## Installation12 13This is a [Node.js](https://nodejs.org/en/) module available through the14[npm registry](https://www.npmjs.com/). Installation is done using the15[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):16 17```sh18$ npm install finalhandler19```20 21## API22 23```js24var finalhandler = require('finalhandler')25```26 27### finalhandler(req, res, [options])28 29Returns function to be invoked as the final step for the given `req` and `res`.30This function is to be invoked as `fn(err)`. If `err` is falsy, the handler will31write out a 404 response to the `res`. If it is truthy, an error response will32be written out to the `res` or `res` will be terminated if a response has already33started.34 35When an error is written, the following information is added to the response:36 37 * The `res.statusCode` is set from `err.status` (or `err.statusCode`). If38 this value is outside the 4xx or 5xx range, it will be set to 500.39 * The `res.statusMessage` is set according to the status code.40 * The body will be the HTML of the status code message if `env` is41 `'production'`, otherwise will be `err.stack`.42 * Any headers specified in an `err.headers` object.43 44The final handler will also unpipe anything from `req` when it is invoked.45 46#### options.env47 48By default, the environment is determined by `NODE_ENV` variable, but it can be49overridden by this option.50 51#### options.onerror52 53Provide a function to be called with the `err` when it exists. Can be used for54writing errors to a central location without excessive function generation. Called55as `onerror(err, req, res)`.56 57## Examples58 59### always 40460 61```js62var finalhandler = require('finalhandler')63var http = require('http')64 65var server = http.createServer(function (req, res) {66 var done = finalhandler(req, res)67 done()68})69 70server.listen(3000)71```72 73### perform simple action74 75```js76var finalhandler = require('finalhandler')77var fs = require('fs')78var http = require('http')79 80var server = http.createServer(function (req, res) {81 var done = finalhandler(req, res)82 83 fs.readFile('index.html', function (err, buf) {84 if (err) return done(err)85 res.setHeader('Content-Type', 'text/html')86 res.end(buf)87 })88})89 90server.listen(3000)91```92 93### use with middleware-style functions94 95```js96var finalhandler = require('finalhandler')97var http = require('http')98var serveStatic = require('serve-static')99 100var serve = serveStatic('public')101 102var server = http.createServer(function (req, res) {103 var done = finalhandler(req, res)104 serve(req, res, done)105})106 107server.listen(3000)108```109 110### keep log of all errors111 112```js113var finalhandler = require('finalhandler')114var fs = require('fs')115var http = require('http')116 117var server = http.createServer(function (req, res) {118 var done = finalhandler(req, res, { onerror: logerror })119 120 fs.readFile('index.html', function (err, buf) {121 if (err) return done(err)122 res.setHeader('Content-Type', 'text/html')123 res.end(buf)124 })125})126 127server.listen(3000)128 129function logerror (err) {130 console.error(err.stack || err.toString())131}132```133 134## License135 136[MIT](LICENSE)137 138[npm-image]: https://img.shields.io/npm/v/finalhandler.svg139[npm-url]: https://npmjs.org/package/finalhandler140[node-image]: https://img.shields.io/node/v/finalhandler.svg141[node-url]: https://nodejs.org/en/download142[coveralls-image]: https://img.shields.io/coveralls/pillarjs/finalhandler.svg143[coveralls-url]: https://coveralls.io/r/pillarjs/finalhandler?branch=master144[downloads-image]: https://img.shields.io/npm/dm/finalhandler.svg145[downloads-url]: https://npmjs.org/package/finalhandler146[github-actions-ci-image]: https://github.com/pillarjs/finalhandler/actions/workflows/ci.yml/badge.svg147[github-actions-ci-url]: https://github.com/pillarjs/finalhandler/actions/workflows/ci.yml148 