Skip to content

Commit cde1109

Browse files
committed
modify & add http-client code
1 parent ee16831 commit cde1109

16 files changed

+218
-51
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
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],
55
headers = { 'User-Agent': '01-my-curl.js' };
66

7+
if(!url) {
8+
log('Usage: cmd url');
9+
process.exit();
10+
}
11+
712
fetch(url, headers)
813
.then(res => {
914
log(`HTTP/1.1 ${res.status} ${res.statusText}`);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

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

8-
http.get(url, {headers}, (res) => {
8+
if(!url) {
9+
log('Usage: cmd url');
10+
process.exit();
11+
}
12+
13+
http.get(url, {headers}, res => {
914
log(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);
1015
log(res.headers);
1116
log('');

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,23 @@ 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

16+
function format(obj) {
17+
return {
18+
'日期': `${obj.ymd} ${obj.week}`,
19+
'最高温': obj.high,
20+
'最低温': obj.low,
21+
'风力': `${obj.fx} ${obj.fl}`,
22+
'状态': obj.type,
23+
};
24+
}
25+
1626
fetch(addr)
1727
.then(res => res.json())
18-
.then(data => console.dir(data, { depth: null, colors: true }));
28+
.then(data => {
29+
data = data.data.forecast;
30+
data = data.map(format);
31+
console.table(data);
32+
});

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,26 @@ 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

18+
function format(obj) {
19+
return {
20+
'日期': `${obj.ymd} ${obj.week}`,
21+
'最高温': obj.high,
22+
'最低温': obj.low,
23+
'风力': `${obj.fx} ${obj.fl}`,
24+
'状态': obj.type,
25+
};
26+
}
27+
1828
http.get(addr, res => {
1929
let data = '';
20-
21-
res.on('data', chunk => data += chunk.toString('utf8'));
30+
res.setEncoding('utf8');
31+
res.on('data', chunk => data+=chunk);
2232
res.on('end', () => {
23-
data = JSON.parse(data);
24-
console.dir(data, { depth: null, colors: true });
33+
data = JSON.parse(data).data.forecast;
34+
data = data.map(format);
35+
console.table(data);
2536
});
2637
});

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
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

9-
https.get(url, {headers}, (res) => {
10-
let result = '';
11-
12-
res.on('data', (data) => result += data.toString('utf8'));
9+
https.get(url, {headers}, res => {
10+
let data = '';
11+
res.setEncoding('utf8');
12+
res.on('data', chunk => data+=chunk);
1313
res.on('end', () => {
14-
let reps = JSON.parse(result);
15-
16-
log('Total:', reps.items.length);
14+
const { items:reps } = JSON.parse(data);
15+
log('Total:', reps.length);
1716
log('==========================');
18-
for(let i=0; i<reps.items.length; i++) {
19-
log('%d\t%s', (i + 1), reps.items[i].name);
20-
}
17+
for(let i=0; i<reps.length; i++)
18+
log(`${i+1}\t${reps[i].name}`);
2119
});
2220
});

16-http-client/04-post.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
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,
7-
method = 'POST',
87
url = 'http://localhost:8080';
98

10-
let req = http.request(url, {method}, (res) => {
9+
if(!msg) {
10+
log('Usage: cmd data');
11+
process.exit();
12+
}
13+
14+
const req = http.request(url, {method: 'POST'}, res => {
1115
log(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);
1216
log(res.headers);
1317
log('');
14-
1518
res.pipe(process.stdout);
1619
});
1720

16-http-client/04-server.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ http.createServer((req, res) => {
88
log(req.headers);
99
log('');
1010
req.pipe(process.stdout);
11-
1211
res.end('OK!');
1312
}).listen(8080);
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+
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();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
3+
/* global Promise: true */
4+
const http = require('https'),
5+
cheerio = require('cheerio'),
6+
log = console.log,
7+
print = require('util').debuglog('app'),
8+
baseURL = 'https://ke.segmentfault.com/';
9+
10+
function getPage() {
11+
const url = baseURL + 'free';
12+
return new Promise(resolve => http.get(url, res => {
13+
print(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);
14+
print(res.headers);
15+
print('');
16+
17+
let html = '';
18+
res.on('data', chunk => html+=chunk);
19+
res.on('end', () => resolve(html));
20+
}));
21+
}
22+
23+
async function getCourseInfo() {
24+
const html = await getPage();
25+
print(html);
26+
27+
const $ = cheerio.load(html);
28+
$('body').find('.card-title>a').each(function(){
29+
const cName = $(this).text(),
30+
cURL = baseURL + $(this).attr('href');
31+
32+
if(cName === '') return;
33+
log(`课程名称:${cName}\n课程网址:${cURL.trim()}\n`);
34+
});
35+
}
36+
37+
getCourseInfo();
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,23 @@ const http = require('https'),
77
baseURL = 'https://ke.segmentfault.com/',
88
url = baseURL + 'free';
99

10-
http.get(url, (res) => {
11-
let result = '';
12-
10+
http.get(url, res => {
1311
print(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);
1412
print(res.headers);
1513
print('');
1614

17-
res.on('data', chunk => result += chunk);
15+
let html = '';
16+
res.on('data', chunk => html += chunk);
1817
res.on('end', () => {
19-
print(result);
18+
print(html);
2019

21-
let $ = cheerio.load(result);
20+
const $ = cheerio.load(html);
2221
$('body').find('.card-title>a').each(function(){
23-
let cName = $(this).text(),
24-
cURL = baseURL + $(this).attr('href');
22+
const cName = $(this).text(),
23+
cURL = baseURL + $(this).attr('href');
2524

2625
if(cName === '') return;
27-
28-
log('课程名称:', cName);
29-
log('课程网址:', cURL.trim());
30-
log('');
26+
log(`课程名称:${cName}\n课程网址:${cURL.trim()}\n`);
3127
});
3228
});
3329
});

0 commit comments

Comments
 (0)