source: node_modules/finalhandler/README.md@ 65b6638

main
Last change on this file since 65b6638 was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.0 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][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## 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```js
24var 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 will
31write out a 404 response to the `res`. If it is truthy, an error response will
32be written out to the `res` or `res` will be terminated if a response has already
33started.
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`). If
38 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` is
41 `'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.env
47
48By default, the environment is determined by `NODE_ENV` variable, but it can be
49overridden by this option.
50
51#### options.onerror
52
53Provide a function to be called with the `err` when it exists. Can be used for
54writing errors to a central location without excessive function generation. Called
55as `onerror(err, req, res)`.
56
57## Examples
58
59### always 404
60
61```js
62var 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 action
74
75```js
76var 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 functions
94
95```js
96var 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 errors
111
112```js
113var 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## License
135
136[MIT](LICENSE)
137
138[npm-image]: https://img.shields.io/npm/v/finalhandler.svg
139[npm-url]: https://npmjs.org/package/finalhandler
140[node-image]: https://img.shields.io/node/v/finalhandler.svg
141[node-url]: https://nodejs.org/en/download
142[coveralls-image]: https://img.shields.io/coveralls/pillarjs/finalhandler.svg
143[coveralls-url]: https://coveralls.io/r/pillarjs/finalhandler?branch=master
144[downloads-image]: https://img.shields.io/npm/dm/finalhandler.svg
145[downloads-url]: https://npmjs.org/package/finalhandler
146[github-actions-ci-image]: https://img.shields.io/github/workflow/status/pillarjs/finalhandler/ci/master?label=ci
147[github-actions-ci-url]: https://github.com/jshttp/pillarjs/finalhandler?query=workflow%3Aci
Note: See TracBrowser for help on using the repository browser.