Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 103 additions & 5 deletions src/content/docs/en/4x/guide/debugging.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ description: Learn how to enable and use debugging logs in Express.js applicatio
---

import Alert from '@components/primitives/Alert/Alert.astro';
import PackageManagerCommand from '@components/patterns/PackageManagerCommand/PackageManagerCommand.astro';

To see all the internal logs used in Express, set the `DEBUG` environment variable to
`express:*` when launching your app.
Expand Down Expand Up @@ -86,22 +87,119 @@ When a request is then made to the app, you will see the logs specified in the E

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.

## Applications generated by `express`
## Using `debug` in your own code

An application generated by the `express` command uses the `debug` module and its debug namespace is scoped to the name of the application.
The same [debug](https://www.npmjs.com/package/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`:

For example, if you generated the app with `$ express sample-app`, you can enable the debug statements with the following command:
<PackageManagerCommand command="npm install debug" />

```cjs title="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');
});
```

```mjs title="index.mjs"
import express from 'express';
import debugModule from 'debug';

const debug = debugModule('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:

```bash
$ DEBUG=sample-app:* node ./bin/www
$ DEBUG=myapp:* node index.js
```

You can specify more than one debug namespace by assigning a comma-separated list of names:

```bash
$ DEBUG=http,mail,express:* node index.js
$ 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:

```json title=".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](https://code.visualstudio.com/docs/nodejs/nodejs-debugging) 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:

```bash
$ 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](https://nodejs.org/en/learn/getting-started/debugging) 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:

```bash
$ 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](https://nodejs.org/api/diagnostics_channel.html#http) module, which you can subscribe to without patching Express or adding middleware:

```cjs title="index.cjs"
const { subscribe } = require('node:diagnostics_channel');

subscribe('http.server.request.start', ({ request }) => {
console.log(`${request.method} ${request.url}`);
});
```

```mjs title="index.mjs"
import { subscribe } from '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](https://nodejs.org/api/diagnostics_channel.html#http) for the full list.

## Advanced options

When running through Node.js, you can set a few environment variables that will change the behavior of the debug logging:
Expand Down
Loading
Loading