mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
doc: sending http request to localhost to avoid https redirect
In the JSON fetching example, http.get request is being sent to an http url that redirects to https. This causes the http.get request to fail. To avoid redirect errors, a local http server is set up that returns a json response. Fixes: https://github.com/nodejs/node/issues/37907 PR-URL: https://github.com/nodejs/node/pull/38036 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Nitzan Uziely <linkgoron@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
56aaf7010c
commit
aff0cd3ea6
1 file changed
+11
-1
+11
-1
@@ -2287,7 +2287,7 @@ The `callback` is invoked with a single argument that is an instance of
|
||||
JSON fetching example:
|
||||
|
||||
```js
|
||||
http.get('http://nodejs.org/dist/index.json', (res) => {
|
||||
http.get('http://localhost:8000/', (res) => {
|
||||
const { statusCode } = res;
|
||||
const contentType = res.headers['content-type'];
|
||||
|
||||
@@ -2322,6 +2322,16 @@ http.get('http://nodejs.org/dist/index.json', (res) => {
|
||||
}).on('error', (e) => {
|
||||
console.error(`Got error: ${e.message}`);
|
||||
});
|
||||
|
||||
// Create a local server to receive data from
|
||||
const server = http.createServer((req, res) => {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
data: 'Hello World!'
|
||||
}));
|
||||
});
|
||||
|
||||
server.listen(8000);
|
||||
```
|
||||
|
||||
## `http.globalAgent`
|
||||
|
||||
Reference in new issue
Block a user