source: trip-planner-front/node_modules/finalhandler/README.md

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.9 KB
Line 
1# finalhandler
2
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][travis-image]][travis-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## Installation
12
13This is a [Node.js](https://nodejs.org/en/) module available through the
14[npm registry](https://www.npmjs.com/). Installation is done using the
15[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
16
17```sh
18$ npm install finalhandler
19```
20
21## API
22
23<!-- eslint-disable no-unused-vars -->
24
25```js
26var finalhandler = require('finalhandler')
27```
28
29### finalhandler(req, res, [options])
30
31Returns function to be invoked as the final step for the given `req` and `res`.
32This function is to be invoked as `fn(err)`. If `err` is falsy, the handler will
33write out a 404 response to the `res`. If it is truthy, an error response will
34be written out to the `res`.
35
36When an error is written, the following information is added to the response:
37
38 * The `res.statusCode` is set from `err.status` (or `err.statusCode`). If
39 this value is outside the 4xx or 5xx range, it will be set to 500.
40 * The `res.statusMessage` is set according to the status code.
41 * The body will be the HTML of the status code message if `env` is
42 `'production'`, otherwise will be `err.stack`.
43 * Any headers specified in an `err.headers` object.
44
45The final handler will also unpipe anything from `req` when it is invoked.
46
47#### options.env
48
49By default, the environment is determined by `NODE_ENV` variable, but it can be
50overridden by this option.
51
52#### options.onerror
53
54Provide a function to be called with the `err` when it exists. Can be used for
55writing errors to a central location without excessive function generation. Called
56as `onerror(err, req, res)`.
57
58## Examples
59
60### always 404
61
62```js
63var finalhandler = require('finalhandler')
64var http = require('http')
65
66var server = http.createServer(function (req, res) {
67 var done = finalhandler(req, res)
68 done()
69})
70
71server.listen(3000)
72```
73
74### perform simple action
75
76```js
77var finalhandler = require('finalhandler')
78var fs = require('fs')
79var http = require('http')
80
81var server = http.createServer(function (req, res) {
82 var done = finalhandler(req, res)
83
84 fs.readFile('index.html', function (err, buf) {
85 if (err) return done(err)
86 res.setHeader('Content-Type', 'text/html')
87 res.end(buf)
88 })
89})
90
91server.listen(3000)
92```
93
94### use with middleware-style functions
95
96```js
97var finalhandler = require('finalhandler')
98var http = require('http')
99var serveStatic = require('serve-static')
100
101var serve = serveStatic('public')
102
103var server = http.createServer(function (req, res) {
104 var done = finalhandler(req, res)
105 serve(req, res, done)
106})
107
108server.listen(3000)
109```
110
111### keep log of all errors
112
113```js
114var finalhandler = require('finalhandler')
115var fs = require('fs')
116var http = require('http')
117
118var server = http.createServer(function (req, res) {
119 var done = finalhandler(req, res, { onerror: logerror })
120
121 fs.readFile('index.html', function (err, buf) {
122 if (err) return done(err)
123 res.setHeader('Content-Type', 'text/html')
124 res.end(buf)
125 })
126})
127
128server.listen(3000)
129
130function logerror (err) {
131 console.error(err.stack || err.toString())
132}
133```
134
135## License
136
137[MIT](LICENSE)
138
139[npm-image]: https://img.shields.io/npm/v/finalhandler.svg
140[npm-url]: https://npmjs.org/package/finalhandler
141[node-image]: https://img.shields.io/node/v/finalhandler.svg
142[node-url]: https://nodejs.org/en/download
143[travis-image]: https://img.shields.io/travis/pillarjs/finalhandler.svg
144[travis-url]: https://travis-ci.org/pillarjs/finalhandler
145[coveralls-image]: https://img.shields.io/coveralls/pillarjs/finalhandler.svg
146[coveralls-url]: https://coveralls.io/r/pillarjs/finalhandler?branch=master
147[downloads-image]: https://img.shields.io/npm/dm/finalhandler.svg
148[downloads-url]: https://npmjs.org/package/finalhandler
Note: See TracBrowser for help on using the repository browser.