跟着教程走还是比较顺畅的,虽然没有面面俱到,但是入门还是可以的。
婴儿学步
1
2
3
4
5
6
7
8
9
|
let paramsArr = process.argv.slice(2);
let result = 0;
paramsArr.forEach(item => {
const i = item - 0;
if (typeof i === 'number') {
result += i;
}
});
console.log(result);
|
第一个 I/O
1
2
3
4
5
6
7
8
9
|
let fs = require('fs');
try {
const data = fs.readFileSync(process.argv[2], 'utf-8');
const dataStr = data.toString();
const strArr = dataStr.split('\n');
console.log(strArr.length - 1);
} catch (err) {
console.error(err);
}
|
第一个异步 I/O
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
let fs = require('fs');
let readOneFile = file =>
new Promise((resolve, reject) => {
fs.readFile(file, 'utf-8', (err, data) => {
if (err) {
reject(err);
}
resolve(data);
});
});
readOneFile(process.argv[2])
.then(data => {
const dataStr = data.toString();
const strArr = dataStr.split('\n');
console.log(strArr.length - 1);
})
.catch(err => new Error(err));
|
LS 过滤器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
const fs = require('fs');
const path = require('path');
const ext = `.${process.argv[3]}`;
let readDir = dir =>
new Promise((resolve, reject) => {
fs.readdir(dir, (err, list) => {
if (err) {
reject(err);
}
resolve(list);
});
});
readDir(process.argv[2])
.then(list => {
list
.filter(item => path.extname(item) === ext)
.forEach(item => {
console.log(item);
});
})
.catch(err => {
console.error(err);
});
|
使其模块化
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
|
// module 1
const subModule = require('./ms.06.p2');
const dir = process.argv[2];
const ext = process.argv[3];
let res = (dir, ext) =>
new Promise((resolve, reject) => {
const cb = (err, data) => {
if (err) {
reject(err);
}
resolve(data);
};
subModule(dir, ext, cb);
});
res(dir, ext)
.then(list => {
list.forEach(item => {
console.log(item);
});
})
.catch(err => {
console.error(err);
});
// module 2
const fs = require('fs');
const path = require('path');
module.exports = (dir, ext, cb) => {
fs.readdir(dir, (err, data) => {
if (err) {
return cb(err);
}
const realExt = `.${ext}`;
const list = data.filter(item => realExt === path.extname(item));
return cb(null, list);
});
};
|
HTTP 客户端
1
2
3
4
5
6
7
|
const http = require('http');
http.get(process.argv[2], res => {
res.setEncoding('utf-8');
res.on('data', data => {
console.log(data);
});
});
|
HTTP 收集器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
const http = require('http');
let result = '';
http.get(process.argv[2], res => {
res.setEncoding('utf-8');
res.on('data', data => {
result = result + data;
});
res.on('end', () => {
console.log(result.length);
console.log(result);
});
res.on('error', err => {
console.error(err);
});
});
|
玩转异步
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
|
const http = require('http');
const getUrl = url =>
new Promise((resolve, reject) => {
let answer = '';
http.get(url, res => {
res.setEncoding('utf-8');
res.on('data', data => {
answer = answer + data;
});
res.on('end', () => {
resolve(answer);
});
res.on('error', err => {
reject(err);
});
});
});
Promise.all([
getUrl(process.argv[2]),
getUrl(process.argv[3]),
getUrl(process.argv[4])
])
.then(res => {
res.forEach(item => {
console.log(item);
});
})
.catch(err => {
console.log(err);
});
|
授时服务器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
const net = require('net');
const time = () => {
let now = new Date();
let y = now.getFullYear();
let m = now.getMonth() + 1;
let d = now.getDate();
let h = now.getHours();
let ms = now.getMinutes();
m = m.toString().length > 1 ? m : `0${m}`;
d = d.toString().length > 1 ? d : `0${d}`;
h = h.toString().length > 1 ? h : `0${h}`;
ms = ms.toString().length > 1 ? ms : `0${ms}`;
const ymd = `${y}-${m}-${d}`;
const hms = `${h}:${ms}`;
return `${ymd} ${hms}\n`;
};
const server = net.createServer(socket => {
socket.end(time());
});
server.listen(process.argv[2]);
|
HTTP 文件服务器
1
2
3
4
5
6
7
8
9
10
11
|
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
let readStream = fs.createReadStream(process.argv[3], {
encoding: 'utf-8'
});
readStream.on('data', data => {
res.write(data);
});
});
server.listen(process.argv[2]);
|
HTTP 大写转换器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
const http = require('http');
const server = http.createServer((req, res) => {
req.setEncoding('utf-8');
let body = '';
if (req.method === 'POST') {
req.on('data', data => {
body = body + data.toUpperCase();
});
req.on('end', () => {
res.write(body);
});
}
});
server.listen(process.argv[2]);
|
HTTP JSON API 服务器
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
|
const http = require('http');
const url = require('url');
const app = http.createServer((req, res) => {
req.setEncoding('utf-8');
if (req.method === 'GET') {
const temp = url.parse(req.url, true);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(job(temp)));
} else {
res.writeHead(405);
res.end();
}
});
function job(url) {
const { pathname, query } = url;
const { iso } = query;
switch (pathname) {
case '/api/parsetime':
return handleParse(iso);
case '/api/unixtime':
return handleUnix(iso);
default:
return null;
}
}
function handleParse(time) {
const t = new Date(time);
return {
hour: t.getHours(),
minute: t.getMinutes(),
second: t.getSeconds()
};
}
function handleUnix(time) {
const t = new Date(time);
return {
unixtime: t.getTime()
};
}
app.listen(process.argv[2]);
|