Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add docs for proxy-pass #2326

Closed
wants to merge 1 commit into from
Closed
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
55 changes: 55 additions & 0 deletions documentation/3-streams.md
Original file line number Diff line number Diff line change
@@ -448,3 +448,58 @@ The [response headers](https://nodejs.org/api/http.html#http_message_headers).
**Type: `string`**

The status message corresponding to the status code.

## Proxy Pass
In certain cases, it may be necessary to employ **`got`** in our router to ensure that all incoming requests to our web server are proxied to another web server. This is akin to utilizing a reverse proxy. To illustrate, the code below demonstrates the usage of `Express`:

```js
import stream from 'node:stream';
import express from 'express';
import got from 'got';

const router = express.Router();

router.get('/proxy-pass', (req, res, next) => {
const readableStream = new stream.PassThrough();
req.pipe(readableStream);

const serverRes = got.stream.get({
'http://example.com',
headers: req.headers,
});

res.set(serverRes.headers);
serverResponse.pipe(res);
});

router.post('/proxy-pass', (req, res, next) => {
const readableStream = new stream.PassThrough();
req.pipe(readableStream);

const serverRes = got.stream.post({
'http://example.com',
body: readableStream,
headers: req.headers,
});

res.set(serverRes.headers);
serverResponse.pipe(res);
});

export default router;

```
- **Creating a PassThrough Stream:**
A new PassThrough stream (`readableStream`) is created.

- **Piping Request to PassThrough Stream:**
The incoming POST request (`req`) is piped into the `readableStream`.

- **Making a Request using `got.stream`:**
It initiates a POST request to 'http://example.com' with the request headers and the body as the PassThrough stream (`readableStream`). The response is a readable stream (`serverRes`).

- **Setting Response Headers:**
The headers from the server response (`serverRes.headers`) are set to the outgoing response (`res`).

- **Piping Server Response to Outgoing Response:**
The server response stream (`serverRes`) is piped into the outgoing response (`res`).