http-errors

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Create HTTP errors for Express, Koa, Connect, etc. with ease.轻松地创建Express、Koa、Connect等的HTTP错误。

Install

This is a Node.js module available through the npm registry. 这是一个Node.js模块,可通过npm注册表获得。Installation is done using the npm install command:使用npm install命令完成安装:

$ npm install http-errors

Example实例

var createError = require('http-errors')
var express = require('express')
var app = express()

app.use(function (req, res, next) {
  if (!req.user) return next(createError(401, 'Please login to view this page.'))
  next()
})

API

This is the current API, currently extracted from Koa and subject to change.这是当前的API,目前从KOA中提取,并有变化。

Error Properties错误属性

createError([status], [message], [properties])

Create a new error object with the given message msg. 使用给定的消息msg创建一个新的错误对象。The error object inherits from createError.HttpError.错误对象继承自createError.HttpError

var err = createError(404, 'This video does not exist!')

createError([status], [error], [properties])

Extend the given error object with createError.HttpError properties. 使用createError.HttpError属性扩展给定的error对象。This will not alter the inheritance of the given error object, and the modified error object is the return value.这不会改变给定错误对象的继承,修改后的error对象是返回值。

fs.readFile('foo.txt', function (err, buf) {
  if (err) {
    if (err.code === 'ENOENT') {
      var httpError = createError(404, err, { expose: false })
    } else {
      var httpError = createError(500, err)
    }
  }
})

createError.isHttpError(val)

Determine if the provided val is an HttpError. 确定提供的val是否为HttpErrorThis will return true if the error inherits from the HttpError constructor of this module or matches the "duck type" for an error this module creates. 如果error继承自此模块的HttpError构造函数,或者与此模块创建的错误的“duck type”匹配,则返回trueAll outputs from the createError factory will return true for this function, including if an non-HttpError was passed into the factory.对于此函数,createError工厂的所有输出将返回true,包括是否将非HttpError传递到工厂。

new createError[code || name]([msg]))

Create a new error object with the given message msg. 使用给定的消息msg创建一个新的错误对象。The error object inherits from createError.HttpError.错误对象继承自createError.HttpError

var err = new createError.NotFound()

List of all constructors所有构造函数清单

Status Code状态码 Constructor Name构造函数名称
400BadRequest
401Unauthorized
402PaymentRequired
403Forbidden
404NotFound
405MethodNotAllowed
406NotAcceptable
407ProxyAuthenticationRequired
408RequestTimeout
409Conflict
410Gone
411LengthRequired
412PreconditionFailed
413PayloadTooLarge
414URITooLong
415UnsupportedMediaType
416RangeNotSatisfiable
417ExpectationFailed
418ImATeapot
421MisdirectedRequest
422UnprocessableEntity
423Locked
424FailedDependency
425UnorderedCollection
426UpgradeRequired
428PreconditionRequired
429TooManyRequests
431RequestHeaderFieldsTooLarge
451UnavailableForLegalReasons
500InternalServerError
501NotImplemented
502BadGateway
503ServiceUnavailable
504GatewayTimeout
505HTTPVersionNotSupported
506VariantAlsoNegotiates
507InsufficientStorage
508LoopDetected
509BandwidthLimitExceeded
510NotExtended
511NetworkAuthenticationRequired

License

MIT