¥Debugging Express
要查看 Express 中使用的所有内部日志,请在启动应用时将 DEBUG
环境变量设置为 express:*
。
¥To see all the internal logs used in Express, set the DEBUG
environment variable to
express:*
when launching your app.
$ DEBUG=express:* node index.js
在 Windows 上,使用相应的命令。
¥On Windows, use the corresponding command.
> set DEBUG=express:* & node index.js
在 express 生成器 生成的默认应用上运行此命令会打印以下输出:
¥Running this command on the default app generated by the express generator prints the following output:
$ DEBUG=express:* node ./bin/www
express:router:route new / +0ms
express:router:layer new / +1ms
express:router:route get / +1ms
express:router:layer new / +0ms
express:router:route new / +1ms
express:router:layer new / +0ms
express:router:route get / +0ms
express:router:layer new / +0ms
express:application compile etag weak +1ms
express:application compile query parser extended +0ms
express:application compile trust proxy false +0ms
express:application booting in development mode +1ms
express:router use / query +0ms
express:router:layer new / +0ms
express:router use / expressInit +0ms
express:router:layer new / +0ms
express:router use / favicon +1ms
express:router:layer new / +0ms
express:router use / logger +0ms
express:router:layer new / +0ms
express:router use / jsonParser +0ms
express:router:layer new / +1ms
express:router use / urlencodedParser +0ms
express:router:layer new / +0ms
express:router use / cookieParser +0ms
express:router:layer new / +0ms
express:router use / stylus +90ms
express:router:layer new / +0ms
express:router use / serveStatic +0ms
express:router:layer new / +0ms
express:router use / router +0ms
express:router:layer new / +1ms
express:router use /users router +0ms
express:router:layer new /users +0ms
express:router use / <anonymous> +0ms
express:router:layer new / +0ms
express:router use / <anonymous> +0ms
express:router:layer new / +0ms
express:router use / <anonymous> +0ms
express:router:layer new / +0ms
当向应用发出请求时,你将看到 Express 代码中指定的日志:
¥When a request is then made to the app, you will see the logs specified in the Express code:
express:router dispatching GET / +4h
express:router query : / +2ms
express:router expressInit : / +0ms
express:router favicon : / +0ms
express:router logger : / +1ms
express:router jsonParser : / +0ms
express:router urlencodedParser : / +1ms
express:router cookieParser : / +0ms
express:router stylus : / +0ms
express:router serveStatic : / +2ms
express:router router : / +2ms
express:router dispatching GET / +1ms
express:view lookup "index.pug" +338ms
express:view stat "/projects/example/views/index.pug" +0ms
express:view render "/projects/example/views/index.pug" +1ms
要仅从路由实现中查看日志,请将 DEBUG
的值设置为 express:router
。同样,要仅从应用实现中查看日志,请将 DEBUG
的值设置为 express:application
,依此类推。
¥To see the logs only from the router implementation, set the value of DEBUG
to express:router
. Likewise, to see logs only from the application implementation, set the value of DEBUG
to express:application
, and so on.
express
生成的应用¥Applications generated by express
由 express
命令生成的应用使用 debug
模块,其调试命名空间的范围为应用的名称。
¥An application generated by the express
command uses the debug
module and its debug namespace is scoped to the name of the application.
例如,如果你使用 $ express sample-app
生成应用,则可以使用以下命令启用调试语句:
¥For example, if you generated the app with $ express sample-app
, you can enable the debug statements with the following command:
$ DEBUG=sample-app:* node ./bin/www
你可以通过分配以逗号分隔的名称列表来指定多个调试命名空间:
¥You can specify more than one debug namespace by assigning a comma-separated list of names:
$ DEBUG=http,mail,express:* node index.js
¥Advanced options
通过 Node.js 运行时,你可以设置一些环境变量来改变调试日志的行为:
¥When running through Node.js, you can set a few environment variables that will change the behavior of the debug logging:
名称 | 目的 |
---|---|
DEBUG |
启用/禁用特定的调试命名空间。 |
DEBUG_COLORS |
是否在调试输出中使用颜色。 |
DEBUG_DEPTH |
对象检查深度。 |
DEBUG_FD |
要将调试输出写入的文件描述符。 |
DEBUG_SHOW_HIDDEN |
显示检查对象的隐藏属性。 |
注意:以 DEBUG_
开头的环境变量最终会转换为与 %o
/%O
格式化程序一起使用的选项对象。有关完整列表,请参阅 util.inspect()
的 Node.js 文档。
¥Note: The environment variables beginning with DEBUG_
end up being
converted into an Options object that gets used with %o
/%O
formatters.
See the Node.js documentation for
util.inspect()
for the complete list.