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]
9 Node.js function to invoke as the final step to respond to HTTP request.
13 This 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):
18 $ npm install finalhandler
23 <!-- eslint-disable no-unused-vars -->
26 var finalhandler = require('finalhandler')
29 ### finalhandler(req, res, [options])
31 Returns function to be invoked as the final step for the given `req` and `res`.
32 This function is to be invoked as `fn(err)`. If `err` is falsy, the handler will
33 write out a 404 response to the `res`. If it is truthy, an error response will
34 be written out to the `res`.
36 When an error is written, the following information is added to the response:
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.
45 The final handler will also unpipe anything from `req` when it is invoked.
49 By default, the environment is determined by `NODE_ENV` variable, but it can be
50 overridden by this option.
54 Provide a function to be called with the `err` when it exists. Can be used for
55 writing errors to a central location without excessive function generation. Called
56 as `onerror(err, req, res)`.
63 var finalhandler = require('finalhandler')
64 var http = require('http')
66 var server = http.createServer(function (req, res) {
67 var done = finalhandler(req, res)
74 ### perform simple action
77 var finalhandler = require('finalhandler')
78 var fs = require('fs')
79 var http = require('http')
81 var server = http.createServer(function (req, res) {
82 var done = finalhandler(req, res)
84 fs.readFile('index.html', function (err, buf) {
85 if (err) return done(err)
86 res.setHeader('Content-Type', 'text/html')
94 ### use with middleware-style functions
97 var finalhandler = require('finalhandler')
98 var http = require('http')
99 var serveStatic = require('serve-static')
101 var serve = serveStatic('public')
103 var server = http.createServer(function (req, res) {
104 var done = finalhandler(req, res)
105 serve(req, res, done)
111 ### keep log of all errors
114 var finalhandler = require('finalhandler')
115 var fs = require('fs')
116 var http = require('http')
118 var server = http.createServer(function (req, res) {
119 var done = finalhandler(req, res, { onerror: logerror })
121 fs.readFile('index.html', function (err, buf) {
122 if (err) return done(err)
123 res.setHeader('Content-Type', 'text/html')
130 function logerror (err) {
131 console.error(err.stack || err.toString())
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