Skip to content

Commit 1b1f643

Browse files
committed
Add examples
1 parent 3db932f commit 1b1f643

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

JavaScript/1-get.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const http = require('http');
4+
5+
const url = 'http://ietf.org/';
6+
7+
http.get(url, res => {
8+
if (res.statusCode !== 200) {
9+
console.log(`Status Code: ${res.statusCode}`);
10+
return;
11+
}
12+
res.setEncoding('utf8');
13+
const lines = [];
14+
res.on('data', chunk => {
15+
lines.push(chunk);
16+
});
17+
res.on('end', () => {
18+
console.log(lines.join());
19+
});
20+
});

JavaScript/2-fetch.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const http = require('http');
4+
5+
const fetch = url => new Promise((resolve, reject) => http.get(url, res => {
6+
if (res.statusCode !== 200) {
7+
reject(`Status Code: ${res.statusCode}`);
8+
return;
9+
}
10+
res.setEncoding('utf8');
11+
const lines = [];
12+
res.on('data', chunk => lines.push(chunk));
13+
res.on('end', () => resolve(lines.join()));
14+
}));
15+
16+
// Usage
17+
18+
fetch('http://ietf.org/')
19+
.then(body => console.log(body))
20+
.catch(err => console.error(err));

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
# HttpRequest
2-
HTTP, XMLHttpRequest, fetch
1+
# HTTP Request, XMLHttpRequest, fetch

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy