Development-only error handler middleware.仅限开发的错误处理程序中间件。
This middleware is only intended to be used in a development environment, as the full error stack traces and internal details of any object passed to this module will be sent back to the client when an error occurs.此中间件仅用于开发环境中,因为当发生错误时,传递到此模块的任何对象的完整错误堆栈跟踪和内部详细信息都将发送回客户端。
When an object is provided to Express as an error, this module will display as much about this object as possible, and will do so by using content negotiation for the response between HTML, JSON, and plain text.当提供一个对象来表示为错误时,该模块将尽可能多地显示关于该对象的信息,并通过对HTML、JSON和纯文本之间的响应使用内容协商来实现。
Error
object, the string provided by the stack
property will be returned in HTML/text responses.Error
对象时,stack
属性提供的字符串将在HTML/文本响应中返回。Error
object, the result of util.inspect will be returned in HTML/text responses.Error
对象时,util.inspect的结果将以HTML/文本响应的形式返回。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 errorhandler
var errorhandler = require('errorhandler')
Create new middleware to handle errors and respond with content negotiation.创建新的中间件以处理错误并通过内容协商进行响应。
Error handler accepts these properties in the options object.错误处理程序在选项对象中接受这些属性。
Provide a function to be called with the error and a string representation of the error. 提供要使用错误调用的函数和错误的字符串表示形式。Can be used to write the error to any desired location, or set to 可用于将错误写入任何所需位置,或设置为false
to only send the error back in the response. false
以仅在响应中发回错误。Called as 以log(err, str, req, res)
where err
is the Error
object, str
is a string representation of the error, req
is the request object and res
is the response object (note, this function is invoked after the response has been written).log(err, str, req, res)
的形式调用,其中err
是Error
对象,str
是错误的字符串表示,req
是请求对象,res
是响应对象(注意,此函数在写入响应后调用)。
The default value for this option is 除非true
unless process.env.NODE_ENV === 'test'
.process.env.NODE_ENV === 'test'
,否则此选项的默认值为true
。
Possible values:可能值:
true
console.error(str)
.console.error(str)
记录错误。false
Basic example of adding this middleware as the error handler only in development with 添加此中间件作为错误处理程序的基本示例仅在使用connect
(express
also can be used in this example).connect
的开发中使用(此示例中也可以使用express
)。
var connect = require('connect')
var errorhandler = require('errorhandler')
var app = connect()
if (process.env.NODE_ENV === 'development') {
// only use in development
app.use(errorhandler())
}
Sometimes you may want to output the errors to a different location than STDERR during development, like a system notification, for example.有时,您可能希望在开发期间将错误输出到与STDERR不同的位置,例如系统通知。
var connect = require('connect')
var errorhandler = require('errorhandler')
var notifier = require('node-notifier')
var app = connect()
if (process.env.NODE_ENV === 'development') {
// only use in development
app.use(errorhandler({ log: errorNotification }))
}
function errorNotification (err, str, req) {
var title = 'Error in ' + req.method + ' ' + req.url
notifier.notify({
title: title,
message: str
})
}