You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.3 KiB

8 years ago
  1. # finalhandler
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Node.js Version][node-image]][node-url]
  5. [![Build Status][travis-image]][travis-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. Node.js function to invoke as the final step to respond to HTTP request.
  8. ## Installation
  9. ```sh
  10. $ npm install finalhandler
  11. ```
  12. ## API
  13. ```js
  14. var finalhandler = require('finalhandler')
  15. ```
  16. ### finalhandler(req, res, [options])
  17. Returns function to be invoked as the final step for the given `req` and `res`.
  18. This function is to be invoked as `fn(err)`. If `err` is falsy, the handler will
  19. write out a 404 response to the `res`. If it is truthy, an error response will
  20. be written out to the `res`, and `res.statusCode` is set from `err.status`.
  21. The final handler will also unpipe anything from `req` when it is invoked.
  22. #### options.env
  23. By default, the environment is determined by `NODE_ENV` variable, but it can be
  24. overridden by this option.
  25. #### options.onerror
  26. Provide a function to be called with the `err` when it exists. Can be used for
  27. writing errors to a central location without excessive function generation. Called
  28. as `onerror(err, req, res)`.
  29. ## Examples
  30. ### always 404
  31. ```js
  32. var finalhandler = require('finalhandler')
  33. var http = require('http')
  34. var server = http.createServer(function (req, res) {
  35. var done = finalhandler(req, res)
  36. done()
  37. })
  38. server.listen(3000)
  39. ```
  40. ### perform simple action
  41. ```js
  42. var finalhandler = require('finalhandler')
  43. var fs = require('fs')
  44. var http = require('http')
  45. var server = http.createServer(function (req, res) {
  46. var done = finalhandler(req, res)
  47. fs.readFile('index.html', function (err, buf) {
  48. if (err) return done(err)
  49. res.setHeader('Content-Type', 'text/html')
  50. res.end(buf)
  51. })
  52. })
  53. server.listen(3000)
  54. ```
  55. ### use with middleware-style functions
  56. ```js
  57. var finalhandler = require('finalhandler')
  58. var http = require('http')
  59. var serveStatic = require('serve-static')
  60. var serve = serveStatic('public')
  61. var server = http.createServer(function (req, res) {
  62. var done = finalhandler(req, res)
  63. serve(req, res, done)
  64. })
  65. server.listen(3000)
  66. ```
  67. ### keep log of all errors
  68. ```js
  69. var finalhandler = require('finalhandler')
  70. var fs = require('fs')
  71. var http = require('http')
  72. var server = http.createServer(function (req, res) {
  73. var done = finalhandler(req, res, {onerror: logerror})
  74. fs.readFile('index.html', function (err, buf) {
  75. if (err) return done(err)
  76. res.setHeader('Content-Type', 'text/html')
  77. res.end(buf)
  78. })
  79. })
  80. server.listen(3000)
  81. function logerror(err) {
  82. console.error(err.stack || err.toString())
  83. }
  84. ```
  85. ## License
  86. [MIT](LICENSE)
  87. [npm-image]: https://img.shields.io/npm/v/finalhandler.svg?style=flat
  88. [npm-url]: https://npmjs.org/package/finalhandler
  89. [node-image]: https://img.shields.io/node/v/finalhandler.svg?style=flat
  90. [node-url]: http://nodejs.org/download/
  91. [travis-image]: https://img.shields.io/travis/pillarjs/finalhandler.svg?style=flat
  92. [travis-url]: https://travis-ci.org/pillarjs/finalhandler
  93. [coveralls-image]: https://img.shields.io/coveralls/pillarjs/finalhandler.svg?style=flat
  94. [coveralls-url]: https://coveralls.io/r/pillarjs/finalhandler?branch=master
  95. [downloads-image]: https://img.shields.io/npm/dm/finalhandler.svg?style=flat
  96. [downloads-url]: https://npmjs.org/package/finalhandler