Debuggen Express
To see all the internal logs used in Express, set the DEBUG environment variable to express:*,router,router:* when launching your app. Routing is handled by the separate router package, so its logs live under the router namespace and are not included in express:*.
$ DEBUG=express:*,router,router:* node index.jsVerwenden Sie unter Windows den entsprechenden Befehl.
> $env:DEBUG = "express:*,router,router:*"; node index.jsRunning this command on a small app with a JSON body parser and a mounted router prints the following output:
const express = require('express');
const app = express();
app.use(express.json());
const users = express.Router();users.get('/', (req, res) => { res.json([]);});
app.use('/users', users);
app.get('/', (req, res) => { res.send('Hello World!');});
app.listen(3000);import express from 'express';
const app = express();
app.use(express.json());
const users = express.Router();users.get('/', (req, res) => { res.json([]);});
app.use('/users', users);
app.get('/', (req, res) => { res.send('Hello World!');});
app.listen(3000);$ DEBUG=express:*,router,router:* node index.js express:application set "x-powered-by" to true +0ms express:application set "etag" to 'weak' +3ms express:application set "etag fn" to [Function: generateETag] +0ms express:application set "env" to 'development' +1ms express:application set "query parser" to 'simple' +0ms express:application set "query parser fn" to [Function: parse] +0ms express:application set "subdomain offset" to 2 +1ms express:application set "trust proxy" to false +0ms express:application set "trust proxy fn" to [Function: trustNone] +1ms express:application booting in development mode +0ms express:application set "view" to [Function: View] +0ms express:application set "views" to '/projects/example/views' +1ms express:application set "jsonp callback name" to 'callback' +0ms router use '/' jsonParser +0ms router:layer new '/' +0ms router:route new '/' +0ms router:layer new '/' +3ms router:route get / +0ms router:layer new '/' +1ms router use '/users' router +4ms router:layer new '/users' +0ms router:route new '/' +1ms router:layer new '/' +0ms router:route get / +1ms router:layer new '/' +1msWenn eine Anfrage an die App gestellt wird, sehen Sie die im Express-Code angegebenen Protokolle:
router dispatching GET /users +712ms router jsonParser : /users +1ms router trim prefix (/users) from url /users +2ms router router /users : /users +0ms router dispatching GET / +0msTo see the logs only from the router implementation, set the value of DEBUG to router,router:*. Um Protokolle nur von der Anwendungsimplementierung zu sehen, setzten Sie den Wert von DEBUG auf express:application, und so weiter.
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:
npm install debugyarn add debugpnpm add debugbun add debugconst 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');});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:
$ DEBUG=myapp:* node index.jsSie können mehr als einen Debug-Namensraum angeben, indem Sie eine kommaseparierte Namensliste zuweisen:
$ DEBUG=myapp:*,router node index.jsSetting 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:
{ "type": "node", "request": "launch", "name": "Launch Express app", "program": "${workspaceFolder}/index.js", "env": { "DEBUG": "express:*,router,router:*" }}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:
$ node --inspect index.jsThen 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:
$ NODE_DEBUG=http node index.jsYou 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:
const { subscribe } = require('node:diagnostics_channel');
subscribe('http.server.request.start', ({ request }) => { console.log(`${request.method} ${request.url}`);});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 for the full list.
Erweiterte Optionen
Wenn Sie über Node.js laufen, können Sie ein paar Umgebungsvariablen festlegen, die das Verhalten der Debug-Protokollierung ändern:
| Name | Zweck |
|---|---|
DEBUG | Aktiviere/deaktiviert bestimmte Debugging-Namensräume. |
| DEBUG_COLORS | Gibt an, ob Farben in der Debug-Ausgabe verwendet werden sollen oder nicht. |
| DEBUG_DEPTH` | Objektüberprüfungstiefe. |
DEBUG_FD | Datei-Deskriptor, in den die Debug-Ausgabe geschrieben wird. |
DEBUG_SHOW_HIDDEN | Zeigt versteckte Eigenschaften auf untersuchten Objekten. |
Note
Die Umgebungsvariablen, die mit DEBUG_ beginnen, werden am Ende in ein Optionsobjekt
konvertiert, das mit %o/%O Formate verwendet wird. Siehe die Dokumentation von Node.js für
util.inspect() für die vollständige
Liste.