Skip to content
Open
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/de/4x/guide/debugging.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ description: Erfahren Sie, wie Sie Debugging-Logs in Express.js-Anwendungen akti
---

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

Um alle internen Logs zu sehen, die in Express verwendet werden, setzen Sie die Umgebungsvariable `DEBUG` auf
`express:*` beim Starten Ihrer App.
Expand Down Expand Up @@ -86,22 +87,119 @@ Wenn eine Anfrage an die App gestellt wird, sehen Sie die im Express-Code angege

Um die Protokolle nur von der Router-Implementierung zu sehen, setzen Sie den Wert `DEBUG` auf `express:router`. Um Protokolle nur von der Anwendungsimplementierung zu sehen, setzten Sie den Wert von `DEBUG` auf `express:application`, und so weiter.

## Anwendungen generiert von `Express`
## Using `debug` in your own code

Eine Anwendung, die durch den Befehl `express generiert` erzeugt wird, benutzt das Modul `debug` und sein Debug-Namensraum wird auf den Namen der Anwendung übertragen.
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`:

Wenn Sie zum Beispiel die App mit `$ express Beispiel-App` erstellt haben, können Sie die Debug-Anweisungen mit folgendem Befehl aktivieren:
<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
```

Sie können mehr als einen Debug-Namensraum angeben, indem Sie eine kommaseparierte Namensliste zuweisen:

```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/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.

## Erweiterte Optionen

Wenn Sie über Node.js laufen, können Sie ein paar Umgebungsvariablen festlegen, die das Verhalten der Debug-Protokollierung ändern:
Expand Down
40 changes: 24 additions & 16 deletions src/content/docs/de/4x/guide/error-handling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,39 @@ app.get('/', (req: Request, res: Response, next: NextFunction) => {
});
```

Starting with Express 5, route handlers and middleware that return a Promise
will call `next(value)` automatically when they reject or throw an error.
Zum Beispiel:
Errors from rejected promises are not passed to `next` automatically, and this includes `async` functions: if an `async` route handler throws or awaits a rejected promise, the rejection is unhandled, which crashes the process on current Node.js versions. You must catch the error yourself and pass it to Express:

```js
app.get('/user/:id', async (req, res, next) => {
const user = await getUserById(req.params.id);
res.send(user);
try {
const user = await getUserById(req.params.id);
res.send(user);
} catch (err) {
next(err);
}
});
```

```ts
import { type Request, type Response, type NextFunction } from 'express';

app.get('/user/:id', async (req: Request, res: Response, next: NextFunction) => {
const user = await getUserById(req.params.id);
res.send(user);
try {
const user = await getUserById(req.params.id);
res.send(user);
} catch (err) {
next(err);
}
});
```

Wenn `getUserById` einen Fehler oder eine Ablehnung, wird `next` entweder mit
dem Wurffehler oder dem abgelehnten Wert aufgerufen. Wenn kein abgelehnter Wert angegeben wird, wird `next`
mit einem Standard-Fehlerobjekt aufgerufen, das vom Express-Router bereitgestellt wird.
<Alert type="info">

Consider [updating to Express 5](/guide/migrating-5), where route handlers and middleware that
return a Promise call `next(value)` automatically when they reject or throw an error, making the
`try...catch` above unnecessary.

</Alert>

Wenn du etwas an die `next()` Funktion übergibt (außer den String `'route'`),
Express betrachtet die aktuelle Anfrage als Fehler und überspringt alle
Expand Down Expand Up @@ -180,9 +190,7 @@ app.get('/', (req: Request, res: Response, next: NextFunction) => {
});
```

Da Versprechungen automatisch sowohl synchrone Fehler als auch abgelehnte Versprechungen fangen,
du kannst einfach `next` angeben, da der letzte Catch Handler und Express Fehler fängt an
weil der Catch-Handler den Fehler als erstes Argument angibt.
If a callback in the promise chain throws, the chain turns that exception into a rejection, which travels down to the final `.catch`. There, passing `next` as the handler is enough: `.catch` calls it with the error as its first argument, which is exactly the argument `next` expects, so the error reaches Express.

Sie können auch eine Kette von Handlern verwenden, um sich auf synchrone Fehler
zu stützen, indem Sie den asynchronen Code auf etwas Triviales reduzieren. Zum Beispiel:
Expand Down Expand Up @@ -219,8 +227,8 @@ app.get('/', [
]);
```

Das obige Beispiel hat ein paar triviale Anweisungen aus dem Aufruf `readFile`
. If `readFile` causes an error, then it passes the error to Express, otherwise you
The above example contains a couple of trivial statements following the `readFile`
call. If `readFile` causes an error, then it passes the error to Express, otherwise you
quickly return to the world of synchronous error handling in the next handler
in the chain. Dann versucht das obige Beispiel die Daten zu verarbeiten. Wenn dies fehlschlägt, fängt der Synchron-Fehlerhandler
ihn ab. Hätten Sie diese Verarbeitung innerhalb von
Expand Down Expand Up @@ -424,7 +432,7 @@ function logErrors(err: Error, req: Request, res: Response, next: NextFunction)

Auch in diesem Beispiel wird `clientErrorHandler` wie folgt definiert; in diesem Fall wird der Fehler explizit an den nächsten weitergegeben.

Beachten Sie, dass, wenn _not_ in einer Fehlerbehandlungsfunktion "Weiter" aufruft, Sie dafür verantwortlich sind, die Antwort zu schreiben (und zu beenden). Andernfalls werden diese Anträge "hängen" und sind nicht für die Müllsammlung berechtigt.
Notice that when you do not call `next` in an error-handling function, you are responsible for writing (and ending) the response. Andernfalls werden diese Anträge "hängen" und sind nicht für die Müllsammlung berechtigt.

```js
function clientErrorHandler(err, req, res, next) {
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/de/4x/guide/overriding-express-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Discover how to customize and extend the Express.js API by overridi

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

Die Express API besteht aus verschiedenen Methoden und Eigenschaften auf den Anfrage- und Antwort-Objekten. Diese werden vom Prototyp geerbt. Es gibt zwei Erweiterungspunkte für die Express API:
The Express API consists of various methods and properties on the [request](/api/request) and [response](/api/response) objects. Diese werden vom Prototyp geerbt. Es gibt zwei Erweiterungspunkte für die Express API:

1. Die globalen Prototypen unter `express.request` und `express.response`.
2. App-spezifische Prototypen bei `app.request` und `app.response`.
Expand Down
Loading