Express框架– Debugging调试

Express使用debug模块在整个请求响应的周内记录route匹配,中间件使用,应用模式,debug 模块是cosole.log的增强版,但是不像console.log(),在产品的代码中你不需要注释掉debug日志,因为默认是关闭的,可以在环境变量中开启DEBUG

2 min read
By myfreax
Express框架– Debugging调试

Express uses the debug module internally to log information about route matches, middleware in use, application mode, and the flow of the request-response cycle.

Express使用debug模块在整个请求响应的周内记录route匹配,中间件使用,应用模式

debug is like an augmented version of console.log. But unlike console.log, you don’t have to comment out debug logs in production code. It is turned off by default and can be conditionally turned on with the use an environment variable named DEBUG.

debug 模块是cosole.log的增强版,但是不像console.log(),在产品的代码中你不需要注释掉debug日志,因为默认是关闭的,可以在环境变量中开启DEBUG

To see all the internal logs used in Express, simply set the DEBUG environment variable to express:* when launching your app.

当启动app的时候,简单的设置环境变量express:*即可查看全部日志

$ DEBUG=express:* node index.js

在window中,使用以下命令

> set DEBUG=express:* & node index.js
$ DEBUG=express:* node ./bin/www
  express:router:route new / +0ms
  express:router:layer new / +1ms
  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

现在,当一个请求到达app时将会看到日志信息

  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.jade" +338ms
  express:view stat "/projects/example/views/index.jade" +0ms
  express:view render "/projects/example/views/index.jade" +1ms

使用Express-generator

通过Express-generator生成一个App

$ express sample-app

开启Debug

$ DEBUG=sample-app node ./bin/www

指定调试模块

$ DEBUG=http,mail,express:* node index.js

Debug http,mail模块