Create HTTP errors for Express, Koa, Connect, etc. with ease.轻松地创建Express、Koa、Connect等的HTTP错误。
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
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()
})
This is the current API, currently extracted from Koa and subject to change.这是当前的API,目前从KOA中提取,并有变化。
expose
- message
should be sent to the client, defaulting to false
when status
>= 500status
>=500时,默认值为false
headers
- undefined
. undefined
。message
- status
- statusCode
for general compatibilitystatusCode
代码以实现一般兼容性statusCode
- 500
500
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!')
status: 500
- message
- 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)
}
}
})
status
- error
- properties
- Determine if the provided 确定提供的val
is an HttpError
. val
是否为HttpError
。This 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”匹配,则返回true
。All outputs from the 对于此函数,createError
factory will return true
for this function, including if an non-HttpError
was passed into the factory.createError
工厂的所有输出将返回true
,包括是否将非HttpError
传递到工厂。
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()
code
- name
- NotFound
or InternalServerError
.NotFound
或InternalServerError
。400 | BadRequest |
401 | Unauthorized |
402 | PaymentRequired |
403 | Forbidden |
404 | NotFound |
405 | MethodNotAllowed |
406 | NotAcceptable |
407 | ProxyAuthenticationRequired |
408 | RequestTimeout |
409 | Conflict |
410 | Gone |
411 | LengthRequired |
412 | PreconditionFailed |
413 | PayloadTooLarge |
414 | URITooLong |
415 | UnsupportedMediaType |
416 | RangeNotSatisfiable |
417 | ExpectationFailed |
418 | ImATeapot |
421 | MisdirectedRequest |
422 | UnprocessableEntity |
423 | Locked |
424 | FailedDependency |
425 | UnorderedCollection |
426 | UpgradeRequired |
428 | PreconditionRequired |
429 | TooManyRequests |
431 | RequestHeaderFieldsTooLarge |
451 | UnavailableForLegalReasons |
500 | InternalServerError |
501 | NotImplemented |
502 | BadGateway |
503 | ServiceUnavailable |
504 | GatewayTimeout |
505 | HTTPVersionNotSupported |
506 | VariantAlsoNegotiates |
507 | InsufficientStorage |
508 | LoopDetected |
509 | BandwidthLimitExceeded |
510 | NotExtended |
511 | NetworkAuthenticationRequired |