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

docs(node17+): dns lookups localhost #783

Merged
merged 8 commits into from
May 30, 2022
Merged
Changes from 5 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
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ app.use(
app.listen(3000);

// proxy and change the base path from "/api" to "/secret"
// http://localhost:3000/api/foo/bar -> http://www.example.org/secret/foo/bar
// http://127.0.0.1:3000/api/foo/bar -> http://www.example.org/secret/foo/bar
```

```typescript
Expand All @@ -67,7 +67,7 @@ app.use(
app.listen(3000);

// proxy and keep the same base path "/api"
// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
// http://127.0.0.1:3000/api/foo/bar -> http://www.example.org/api/foo/bar
```

_All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#options) can be used, along with some extra `http-proxy-middleware` [options](#options).
Expand All @@ -81,7 +81,7 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
- [Express Server Example](#express-server-example)
- [app.use(path, proxy)](#appusepath-proxy)
- [Options](#options)
- [`pathFilter` (string, []string, glob, []glob, function)](#pathfilter-string-string-glob-glob-function)
- [`pathFilter` (string, \[\]string, glob, \[\]glob, function)](#pathfilter-string-string-glob-glob-function)
- [`pathRewrite` (object/function)](#pathrewrite-objectfunction)
- [`router` (object/function)](#router-objectfunction)
- [`plugins` (Array)](#plugins-array)
Expand All @@ -93,6 +93,7 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
- [External WebSocket upgrade](#external-websocket-upgrade)
- [Intercept and manipulate requests](#intercept-and-manipulate-requests)
- [Intercept and manipulate responses](#intercept-and-manipulate-responses)
- [Node.js 17+: Issues over IPv6 and localhost (#705)](#nodejs-17-issues-over-ipv6-and-localhost-705)
- [Debugging](#debugging)
- [Working examples](#working-examples)
- [Recipes](#recipes)
Expand Down Expand Up @@ -257,22 +258,22 @@ Re-target `option.target` for specific requests.
// Use `host` and/or `path` to match requests. First match will be used.
// The order of the configuration matters.
router: {
'integration.localhost:3000' : 'http://localhost:8001', // host only
'staging.localhost:3000' : 'http://localhost:8002', // host only
'localhost:3000/api' : 'http://localhost:8003', // host + path
'/rest' : 'http://localhost:8004' // path only
'integration.localhost:3000' : 'http://127.0.0.1:8001', // host only
'staging.localhost:3000' : 'http://127.0.0.1:8002', // host only
'localhost:3000/api' : 'http://127.0.0.1:8003', // host + path
'/rest' : 'http://127.0.0.1:8004' // path only
}

// Custom router function (string target)
router: function(req) {
return 'http://localhost:8004';
return 'http://127.0.0.1:8004';
}

// Custom router function (target object)
router: function(req) {
return {
protocol: 'https:', // The : is required
host: 'localhost',
host: '127.0.0.1',
port: 8004
};
}
Expand Down Expand Up @@ -488,7 +489,7 @@ The following options are provided by the underlying [http-proxy](https://github
req,
res,
{
target: 'http://localhost:4003/',
target: 'http://127.0.0.1:4003/',
buffer: streamify(req.rawBody),
},
next
Expand Down Expand Up @@ -573,6 +574,16 @@ const proxy = createProxyMiddleware({

Check out [interception recipes](https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/response-interceptor.md#readme) for more examples.

## Node.js 17+: Issues over IPv6 and localhost ([#705](https://github.com/chimurai/http-proxy-middleware/issues/705))

From Node.js 17+: `For dns lookups, Node.js no longer prefers IPv4 over IPV6`.
chimurai marked this conversation as resolved.
Show resolved Hide resolved
E.g. It's **not** guaranteed `localhost` will be resolved to `127.0.0.1`. (Can be either `127.0.0.1`, `::1` or some other IP-address).

To solve it:

- Change `target: http://localhost` to `target: http://127.0.0.1` (IPv4).
- Add this flag when running node locally: `node index.js --dns-result-order=ipv4first`. (Not recommended)

davidmeirlevy marked this conversation as resolved.
Show resolved Hide resolved
## Debugging

Configure the `DEBUG` environment variable enable debug logging.
Expand Down