調試 Express

Express 內部使用 debug 模塊記錄路由匹配、使用到的中間件、應用模式以及請求-響應循環。html

debug 有點像改裝過的 console.log,不一樣的是,您不須要在生產代碼中註釋掉 debug。它會默認關閉,並且使用一個名爲 DEBUG 的環境變量還能夠打開。node

在啓動應用時,設置 DEBUG 環境變量爲 express:*,能夠查看 Express 中用到的全部內部日誌。git

$ DEBUG=express:* node index.js

在 Windows 系統裏,使用以下的命令。github

> set DEBUG=express:* & node index.js

在由 express 應用生成器 生成的默認應用中執行,會打印出以下信息:express

$ 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 代碼中打印出的日誌。json

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

設置 DEBUG 的值爲 express:router,只查看路由部分的日誌;設置 DEBUG 的值爲 express:application,只查看應用部分的日誌,依此類推。cookie

經過 express 生成應用

經過 express 命令行生成的應用也使用了 debug 模塊,它的命名空間限制在應用中。app

若是您經過下述命令生成應用:url

$ express sample-app

則可經過下述命令打開調試信息:spa

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

可經過逗號隔開的名字列表來指定多個調試命名空間,以下所示:

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

請查閱 調試指南 獲取更多有關 debug 的文檔。

相關文章
相關標籤/搜索