finalhandler

中文文档
2.1.1发布于 2025/12/02MIT近 30 天下载 6.6亿

Node.js final http responder

原始使用文档
文档目录9 节
  1. Installation
  2. API
  3. finalhandler(req, res, [options])
  4. Examples
  5. always 404
  6. perform simple action
  7. use with middleware-style functions
  8. keep log of all errors
  9. License

finalhandler

NPM Version NPM Downloads Node.js Version Build Status Test Coverage OpenSSF Scorecard Badge

Node.js function to invoke as the final step to respond to HTTP request.

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install finalhandler

API

const finalhandler = require('finalhandler')

finalhandler(req, res, [options])

Returns function to be invoked as the final step for the given req and res. This function is to be invoked as fn(err). If err is falsy, the handler will write out a 404 response to the res. If it is truthy, an error response will be written out to the res or res will be terminated if a response has already started.

When an error is written, the following information is added to the response:

  • The res.statusCode is set from err.status (or err.statusCode). If this value is outside the 4xx or 5xx range, it will be set to 500.
  • The res.statusMessage is set according to the status code.
  • The body will be the HTML of the status code message if env is 'production', otherwise will be err.stack.
  • Any headers specified in an err.headers object.

The final handler will also unpipe anything from req when it is invoked.

options.env

By default, the environment is determined by NODE_ENV variable, but it can be overridden by this option.

options.onerror

Provide a function to be called with the err when it exists. Can be used for writing errors to a central location without excessive function generation. Called as onerror(err, req, res).

Examples

always 404

const finalhandler = require('finalhandler')
const http = require('http')

const server = http.createServer((req, res) => {
  const done = finalhandler(req, res)
  done()
})

server.listen(3000)

perform simple action

const finalhandler = require('finalhandler')
const fs = require('fs')
const http = require('http')

const server = http.createServer((req, res) => {
  const done = finalhandler(req, res)

  fs.readFile('index.html', (err, buf) => {
    if (err) return done(err)
    res.setHeader('Content-Type', 'text/html')
    res.end(buf)
  })
})

server.listen(3000)

use with middleware-style functions

const finalhandler = require('finalhandler')
const http = require('http')
const serveStatic = require('serve-static')

const serve = serveStatic('public')

const server = http.createServer((req, res) => {
  const done = finalhandler(req, res)
  serve(req, res, done)
})

server.listen(3000)

keep log of all errors

const finalhandler = require('finalhandler')
const fs = require('fs')
const http = require('http')

const server = http.createServer((req, res) => {
  const done = finalhandler(req, res, { onerror: logerror })

  fs.readFile('index.html', (err, buf) => {
    if (err) return done(err)
    res.setHeader('Content-Type', 'text/html')
    res.end(buf)
  })
})

server.listen(3000)

function logerror (err) {
  console.error(err.stack || err.toString())
}

License

MIT

版本与发布记录

finalhandler 共发布 35 个版本,首次发布于 2014/06/06;过去 12 个月发布了 2 个版本。

版本发布日期距今
2.1.1latest2025/12/0211 个月前
1.3.2latest-1.x2025/12/0111 个月前
2.1.02025/03/051.7 年前
1.3.12024/09/122 年前
1.3.02024/09/032 年前
2.0.02024/09/032 年前

关于 finalhandler 的常见问题

finalhandler 是什么?

Node.js final http responder

如何安装 finalhandler?

在项目目录运行 npm install finalhandler,或使用 pnpm add / yarn add / bun add。

finalhandler 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/finalhandler 是否存在。

finalhandler 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

finalhandler 可以商用吗?

finalhandler 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

finalhandler 的下载量有多少?

根据 npm 官方下载统计,finalhandler 在 2026/10/06 – 2026/11/04 这 30 天内被下载 655,434,460 次,日均约 21,847,815 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

finalhandler 还在维护吗?

最新版本 2.1.1 发布于 2025/12/02(11 个月前);至今共发布 35 个版本,其中过去 12 个月发布了 2 个;首次发布于 2014/06/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。