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.
14 $ npm install finalhandler
20 var finalhandler = require('finalhandler')
23 ### finalhandler(req, res, [options])
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`.
30 The final handler will also unpipe anything from `req` when it is invoked.
34 By default, the environment is determined by `NODE_ENV` variable, but it can be
35 overridden by this option.
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)`.
48 var finalhandler = require('finalhandler')
49 var http = require('http')
51 var server = http.createServer(function (req, res) {
52 var done = finalhandler(req, res)
59 ### perform simple action
62 var finalhandler = require('finalhandler')
63 var fs = require('fs')
64 var http = require('http')
66 var server = http.createServer(function (req, res) {
67 var done = finalhandler(req, res)
69 fs.readFile('index.html', function (err, buf) {
70 if (err) return done(err)
71 res.setHeader('Content-Type', 'text/html')
79 ### use with middleware-style functions
82 var finalhandler = require('finalhandler')
83 var http = require('http')
84 var serveStatic = require('serve-static')
86 var serve = serveStatic('public')
88 var server = http.createServer(function (req, res) {
89 var done = finalhandler(req, res)
96 ### keep log of all errors
99 var finalhandler = require('finalhandler')
100 var fs = require('fs')
101 var http = require('http')
103 var server = http.createServer(function (req, res) {
104 var done = finalhandler(req, res, {onerror: logerror})
106 fs.readFile('index.html', function (err, buf) {
107 if (err) return done(err)
108 res.setHeader('Content-Type', 'text/html')
115 function logerror(err) {
116 console.error(err.stack || err.toString())
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