Skip to content

Commit 26eb4c7

Browse files
committed
modify & add http-client code
1 parent 1afa2bd commit 26eb4c7

12 files changed

+100
-24
lines changed

16-http-client/01-my-curl-by-fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
const log = console.log,
4-
url = process.argv[2] || 'http://sample.wangding.co/web/one-div.html',
4+
url = process.argv[2] ?? 'http://sample.wangding.co/web/one-div.html',
55
headers = { 'User-Agent': '01-my-curl.js' };
66

77
fetch(url, headers)

16-http-client/02-get-weather-by-fetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const cityCode = {
1010
'石家庄': 101090101,
1111
};
1212

13-
const city = process.argv[2] || '石家庄',
13+
const city = process.argv[2] ?? '石家庄',
1414
addr = 'http://t.weather.sojson.com/api/weather/city/' + cityCode[city];
1515

1616
fetch(addr)
1717
.then(res => res.json())
18-
.then(data => console.dir(data, { depth: null, colors: true }));
18+
.then(data => console.dir(data, {depth: null}));

16-http-client/02-get-weather.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ const cityCode = {
1212
'石家庄': 101090101,
1313
};
1414

15-
const city = process.argv[2] || '石家庄',
15+
const city = process.argv[2] ?? '石家庄',
1616
addr = 'http://t.weather.sojson.com/api/weather/city/' + cityCode[city];
1717

1818
http.get(addr, res => {
1919
let data = '';
2020

21-
res.on('data', chunk => data += chunk.toString('utf8'));
22-
res.on('end', () => {
23-
data = JSON.parse(data);
24-
console.dir(data, { depth: null, colors: true });
25-
});
21+
res.setEncoding('utf8');
22+
res.on('data', chunk => data+=chunk);
23+
res.on('end', () => console.dir(JSON.parse(data), {depth: null}));
2624
});

16-http-client/03-get-repos.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
const https = require('https');
44

5-
const url = 'https://api.github.com/search/repositories?q=user:' + (process.argv[2] || 'wangding'),
5+
const url = 'https://api.github.com/search/repositories?q=user:' + (process.argv[2] ?? 'wangding'),
66
log = console.log,
77
headers = { 'User-Agent': '02-get-repos.js' };
88

99
https.get(url, {headers}, (res) => {
10-
let result = '';
10+
let data = '';
1111

12-
res.on('data', (data) => result += data.toString('utf8'));
12+
res.setEncoding('utf8');
13+
res.on('data', chunk => data+=chunk);
1314
res.on('end', () => {
14-
let reps = JSON.parse(result);
15+
const { items:reps } = JSON.parse(data);
1516

16-
log('Total:', reps.items.length);
17+
log('Total:', reps.length);
1718
log('==========================');
18-
for(let i=0; i<reps.items.length; i++) {
19-
log('%d\t%s', (i + 1), reps.items[i].name);
20-
}
19+
for(let i=0; i<reps.length; i++)
20+
log(`${i+1}\t${reps[i].name}`);
2121
});
2222
});

16-http-client/04-post.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
const http = require('http');
44

5-
const msg = process.argv[2] || 'Hello! I am wangding.',
5+
const msg = process.argv[2],
66
log = console.log,
77
method = 'POST',
88
url = 'http://localhost:8080';
99

10+
if(!msg) {
11+
log('Usage: cmd data');
12+
process.exit();
13+
}
14+
1015
let req = http.request(url, {method}, (res) => {
1116
log(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);
1217
log(res.headers);

16-http-client/05-get-book-cover.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env node
2+
3+
const http = require('http'),
4+
fs = require('fs');
5+
6+
function getURLs() {
7+
const fileName = './book-1.json';
8+
let books = fs.readFileSync(fileName, 'utf8');
9+
books = JSON.parse(books).UpdateComicItems;
10+
return books.map(book => book.ShowConver);
11+
}
12+
13+
async function getBookCover() {
14+
let i = 1;
15+
getURLs().forEach(url => http.get(url, res => res.pipe(fs.createWriteStream(`./cover/${i++}.jpg`))));
16+
}
17+
18+
getBookCover();

16-http-client/05-crawler.js renamed to 16-http-client/05-get-course-info.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ const http = require('https'),
88
url = baseURL + 'free';
99

1010
http.get(url, (res) => {
11-
let result = '';
11+
let data = '';
1212

1313
print(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);
1414
print(res.headers);
1515
print('');
1616

17-
res.on('data', chunk => result += chunk);
17+
res.on('data', chunk => data += chunk);
1818
res.on('end', () => {
19-
print(result);
19+
print(data);
2020

21-
let $ = cheerio.load(result);
21+
let $ = cheerio.load(data);
2222
$('body').find('.card-title>a').each(function(){
2323
let cName = $(this).text(),
2424
cURL = baseURL + $(this).attr('href');

16-http-client/06-fake-server.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env node
2+
3+
const http = require('http'),
4+
log = console.log;
5+
6+
http.createServer((req, res) => {
7+
log(`${req.method} ${req.url} HTTP/${req.httpVersion}`);
8+
log(req.headers);
9+
log();
10+
11+
if(req.url == '/') {
12+
res.statusCode = 301;
13+
res.setHeader('location', '/abc');
14+
res.end('Moved Permanently');
15+
} else if(req.url == '/abc') {
16+
res.statusCode = 301;
17+
res.setHeader('location', '/def');
18+
res.end('Moved Permanently');
19+
} else if(req.url == '/def') {
20+
res.statusCode = 200;
21+
res.end('<body><h1>hello world</h1></body>');
22+
} else {
23+
res.end('not found');
24+
}
25+
}).listen(8080);

16-http-client/06-redirection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const http = require('http'),
44
url = require('url'),
55
log = console.log;
66

7-
let addr = process.argv[2] || 'http://www.sian.com/';
7+
let addr = 'http://localhost:8080';
88

99
function opt(addr) {
1010
let options = url.parse(addr);
@@ -20,7 +20,7 @@ function get(options) {
2020
log('');
2121

2222
if(res.statusCode < 400 && res.statusCode >= 300) {
23-
get(opt(res.headers.location));
23+
get(opt(addr+res.headers.location));
2424
} else {
2525
res.pipe(process.stdout);
2626
}

16-http-client/07-json.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env node
2+
3+
// curl http://t.weather.sojson.com/api/weather/city/101330101 | ./07-json.js
4+
5+
const stdin = process.stdin;
6+
7+
let data = '';
8+
stdin.setEncoding('utf8');
9+
stdin.on('data', chunk => data+=chunk);
10+
stdin.on('end', ()=>console.dir(JSON.parse(data), {depth:null}));

16-http-client/08-transform.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env node
2+
3+
// curl http://t.weather.sojson.com/api/weather/city/101330101 | ./08-json.js
4+
5+
const stdin = process.stdin,
6+
stdout = process.stdout;
7+
8+
stdin.setEncoding('utf8');
9+
10+
const {Transform} = require('stream');
11+
12+
let json = new Transform();
13+
14+
json._transform = function(chunk, encoding, callback) {
15+
this.push(JSON.stringify(JSON.parse(chunk), null, 2));
16+
callback();
17+
};
18+
19+
stdin.pipe(json).pipe(stdout);

16-http-client/book-1.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)