调试Express
Express在内部运用debug模块来纪录有关路由婚配、正在运用的中间件函数、运用程序形式以及要求—响应周期流的信息。
debug就像是
console.log
的加强版本,但与
console.log
差别,你没必要在临盆代码中解释掉debug日记,默许情况下,日记纪录处于封闭状况,能够运用
DEBUG
环境变量有条件地翻开日记纪录。
要检察Express中运用的一切内部日记,请在启动运用程序时将DEBUG
环境变量设置为express:*
。
$ DEBUG=express:* node index.js
在Windows上,运用响应的敕令。
> set DEBUG=express:* & node index.js
在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代码中指定的日记:
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
,依此类推。
express天生的运用程序
express敕令天生的运用程序也运用debug
模块,其debug的定名空间的局限限定为运用程序的称号。
比方,假如你运用$ express sample-app
天生运用程序,则能够运用以下敕令启用调试语句:
$ DEBUG=sample-app:* node ./bin/www
你能够经由过程分派以逗号分开的称号列表来指定多个调试定名空间:
$ DEBUG=http,mail,express:* node index.js
有关调试的更多信息,请参阅debug。