-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
57 lines (53 loc) · 1.65 KB
/
fetch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var dns = require('node:dns');
dns.setDefaultResultOrder('ipv4first');
async function fetch200Endpoint() {
try {
const response = await fetch('http://localhost:3000/200');
if (response.ok) {
const data = await response.text();
console.log('200 endpoint response:', data);
} else {
console.log('200 endpoint didn\'t return with 200 status');
console.error('Error:', response.status, response.statusText);
}
} catch (error) {
console.log('200 endpoint failed');
console.error('Error:', error);
}
}
async function fetch407Endpoint() {
try {
const response = await fetch('http://localhost:3000/407');
if (response.status === 407) {
const data = await response.text();
console.log('407 endpoint response:', data);
} else {
console.log('407 endpoint didn\'t return with 407 status');
console.error('Error:', response.status, response.statusText);
}
} catch (error) {
console.log('407 endpoint failed');
console.error('Error:', error);
}
}
async function fetch421Endpoint() {
try {
const response = await fetch('http://localhost:3000/421', {
method: 'POST',
body: new Uint8Array(Buffer.from('helloworld', 'utf-8'))
});
if (response.status === 421) {
const data = await response.text();
console.log('421 endpoint response:', data);
} else {
console.log('421 endpoint didn\'t return with 421 status');
console.error('Error:', response.status, response.statusText);
}
} catch (error) {
console.log('421 endpoint failed');
console.error('Error:', error);
}
}
fetch200Endpoint();
fetch407Endpoint();
fetch421Endpoint();