このページを翻訳

Expressのデバッグ

Express で使用されるすべての内部ログを表示するには、アプリを起動するときに DEBUG 環境変数を express:* に設定します。

Terminal window
$ DEBUG=express:* node index.js

Windows では、対応するコマンドを使用します。

Terminal window
> $env:DEBUG = "express:*"; node index.js

express generator で生成されたデフォルトのアプリでこのコマンドを実行すると、以下の出力が出力されます。

Terminal window
$ 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 コードで指定されたログが表示されます。

Terminal window
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などに設定します。同様に、アプリケーションの実装からのログのみを見るには、DEBUGの値をexpress:applicationなどに設定します。

Using debug in your own code

The same debug module that Express uses internally is available for your application code. Install it, create one or more loggers scoped to a namespace of your choosing, and call them wherever you would otherwise use console.log:

Terminal window
npm install debug
index.cjs
const express = require('express');
const debug = require('debug')('myapp:server');
const app = express();
app.get('/', (req, res) => {
debug('handling request from %s', req.ip);
res.send('Hello World!');
});
app.listen(3000, () => {
debug('listening on port 3000');
});

These statements print nothing by default. Enable them through the same DEBUG environment variable, using your own namespace:

Terminal window
$ DEBUG=myapp:* node index.js

カンマ区切りの名前のリストを割り当てることで、複数のデバッグ名前空間を指定できます:

Terminal window
$ DEBUG=myapp:*,express:router node index.js

Setting DEBUG in your IDE

The DEBUG environment variable can be set by any process launcher that supports environment variables, including the run and debug configurations of your IDE. For example, in VS Code you can set it in the env property of a launch configuration:

.vscode/launch.json
{
"type": "node",
"request": "launch",
"name": "Launch Express app",
"program": "${workspaceFolder}/index.js",
"env": { "DEBUG": "express:*" }
}

See Node.js debugging in VS Code for the full list of launch options.

Using the Node.js inspector

Debug logs show what the app did; for stepping through your code with breakpoints, use the built-in Node.js inspector. Start your app with the --inspect flag:

Terminal window
$ node --inspect index.js

Then attach a debugging client, such as Chrome DevTools (open chrome://inspect), VS Code, or any other inspector-capable tool, to set breakpoints in your route handlers and middleware, step through code, and inspect variables.

If you need to debug something that happens during startup, use --inspect-brk instead, which pauses execution on the first line until a debugger attaches. See the Node.js debugging guide for details.

Debugging the HTTP layer

Express runs on top of the Node.js http module, which has its own debugging facilities that work with any Express app.

Setting the NODE_DEBUG environment variable to http makes Node.js print internal logs from the HTTP layer, such as connection handling and socket events:

Terminal window
$ NODE_DEBUG=http node index.js

You can combine it with other subsystems, such as net or stream, in a comma-separated list. Be aware that this output can expose sensitive data such as authentication headers, so use it only in development.

Diagnostics channels

Node.js publishes an event for every HTTP request through the diagnostics_channel module, which you can subscribe to without patching Express or adding middleware:

index.cjs
const { subscribe } = require('node:diagnostics_channel');
subscribe('http.server.request.start', ({ request }) => {
console.log(`${request.method} ${request.url}`);
});

Other built-in channels cover the rest of the request lifecycle, such as http.server.response.finish, and the client side of http.request calls. See the diagnostics_channel documentation for the full list.

高度なオプション

Node.js を介して実行する場合、デバッグログの動作を変更するいくつかの環境変数を設定できます。

名前目的
DEBUG特定のデバッグ名前空間を有効/無効にします。
DEBUG_COLORSデバッグ出力で色を使用するかどうか。
DEBUG_DEPTH物体検査の深さ。
DEBUG_FDデバッグ出力を書き込むファイル記述子。
DEBUG_SHOW_HIDDEN検査対象オブジェクトに非表示のプロパティを表示します。

The environment variables beginning with DEBUG_ end up being converted into an Options object that gets used with %o/%O formatters. util.inspect() の完全な リストについては Node.js のドキュメントを参照してください。