Initial commit for OpenECOMP SDN-C OA&M
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / express / node_modules / finalhandler / README.md
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
9 Node.js function to invoke as the final step to respond to HTTP request.
10
11 ## Installation
12
13 ```sh
14 $ npm install finalhandler
15 ```
16
17 ## API
18
19 ```js
20 var finalhandler = require('finalhandler')
21 ```
22
23 ### finalhandler(req, res, [options])
24
25 Returns function to be invoked as the final step for the given `req` and `res`.
26 This function is to be invoked as `fn(err)`. If `err` is falsy, the handler will
27 write out a 404 response to the `res`. If it is truthy, an error response will
28 be written out to the `res`, and `res.statusCode` is set from `err.status`.
29
30 The final handler will also unpipe anything from `req` when it is invoked.
31
32 #### options.env
33
34 By default, the environment is determined by `NODE_ENV` variable, but it can be
35 overridden by this option.
36
37 #### options.onerror
38
39 Provide a function to be called with the `err` when it exists. Can be used for
40 writing errors to a central location without excessive function generation. Called
41 as `onerror(err, req, res)`.
42
43 ## Examples
44
45 ### always 404
46
47 ```js
48 var finalhandler = require('finalhandler')
49 var http = require('http')
50
51 var server = http.createServer(function (req, res) {
52   var done = finalhandler(req, res)
53   done()
54 })
55
56 server.listen(3000)
57 ```
58
59 ### perform simple action
60
61 ```js
62 var finalhandler = require('finalhandler')
63 var fs = require('fs')
64 var http = require('http')
65
66 var server = http.createServer(function (req, res) {
67   var done = finalhandler(req, res)
68
69   fs.readFile('index.html', function (err, buf) {
70     if (err) return done(err)
71     res.setHeader('Content-Type', 'text/html')
72     res.end(buf)
73   })
74 })
75
76 server.listen(3000)
77 ```
78
79 ### use with middleware-style functions
80
81 ```js
82 var finalhandler = require('finalhandler')
83 var http = require('http')
84 var serveStatic = require('serve-static')
85
86 var serve = serveStatic('public')
87
88 var server = http.createServer(function (req, res) {
89   var done = finalhandler(req, res)
90   serve(req, res, done)
91 })
92
93 server.listen(3000)
94 ```
95
96 ### keep log of all errors
97
98 ```js
99 var finalhandler = require('finalhandler')
100 var fs = require('fs')
101 var http = require('http')
102
103 var server = http.createServer(function (req, res) {
104   var done = finalhandler(req, res, {onerror: logerror})
105
106   fs.readFile('index.html', function (err, buf) {
107     if (err) return done(err)
108     res.setHeader('Content-Type', 'text/html')
109     res.end(buf)
110   })
111 })
112
113 server.listen(3000)
114
115 function logerror(err) {
116   console.error(err.stack || err.toString())
117 }
118 ```
119
120 ## License
121
122 [MIT](LICENSE)
123
124 [npm-image]: https://img.shields.io/npm/v/finalhandler.svg?style=flat
125 [npm-url]: https://npmjs.org/package/finalhandler
126 [node-image]: https://img.shields.io/node/v/finalhandler.svg?style=flat
127 [node-url]: http://nodejs.org/download/
128 [travis-image]: https://img.shields.io/travis/pillarjs/finalhandler.svg?style=flat
129 [travis-url]: https://travis-ci.org/pillarjs/finalhandler
130 [coveralls-image]: https://img.shields.io/coveralls/pillarjs/finalhandler.svg?style=flat
131 [coveralls-url]: https://coveralls.io/r/pillarjs/finalhandler?branch=master
132 [downloads-image]: https://img.shields.io/npm/dm/finalhandler.svg?style=flat
133 [downloads-url]: https://npmjs.org/package/finalhandler