Node内置模块

Node内置模块

path:路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const path  = require('path')
//获取路径信息
const filepath = './1/2/3.text'
console.log(path.dirname(filepath))./1/2
console.log(path.basename(filepath))3.text
console.log(path.extname(filepath)).text

//join路径拼接
const path1 = '/hello'
const path2 = '/world'
const filepath = path.join(path1,path2)

//resolve拼接
const path1 = '/a'
const path2 = '/b'
const dirname = path.resolve(path1,path2)

resolve与join的区别
resolve会帮助查询拼接字符串中有没有最前方的路径拼接符 '/user'的斜杠, 如果没有自动识别全部路径

htttp:

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

// ① 导入 http 模块
const http = require('http');
// ② 创建 web 服务器实例
const server = http.createServer();
// ③ 为服务器实例绑定 request 事件,监听客户端的请求
server.on('request', (req, res) => {
console.log('Someone visit my server.');
});
// ④ 启动服务器
server.listen(3000, function() {
console.log('server strat');
})



const http = require('http');

const server = http.createServer();
server.on('request', (req, res) => {
//req 是请求对象,它包含了与客户端相关的数据或属性
//req.url 客户端请求的地址
const url = req.url;
//req.method 请求的方法
const method = req.method;
const str = `Your request url is ${url},and request method is ${method}`;
console.log(str);
})

server.listen(3000, () => {
console.log('server strat');
})



const http = require('http')

const server = http.createServer();
server.on('request', (req, res) => {
const str = `请求的地址是${req.url},请求的方式是${req.method}`;
//解决中文乱码问题:调用res.setHeader()方法,设置Content-Type
res.setHeader('Content-Type', 'text/html;charset=utf-8');
res.end(str);
})
server.listen(3000, () => {
console.log('server strat');
})



const http = require('http');

const server = http.createServer();
server.on('request', (req, res) => {
// ① 获取请求的 url 地址
const url = req.url;
// ② 设置默认的响应内容为 404 Not found
let content = `<h1>404 Not found!</h1>`;
// ③ 判断用户请求的是否为 / 或 /index.html 首页
// ④ 判断用户请求的是否为 /about.html 关于页面
if (url == '/' || url == '/index.html') {
content = `<h1>首页</h1>`
} else if (url == '/about.html') {
content = `<h1>关于页面</h1>`
}
// ⑤ 设置 Content-Type 响应头,防止中文乱码
res.setHeader('Content-Type', 'text/html;charset=utf-8');
// ⑥ 使用 res.end() 把内容响应给客户端
res.end(content);
})
server.listen(3000, () => {
console.log('running at http://127.0.0.1:3000');
})

fs:

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//文件系统
const fs = require('fs')

//同步读取
const fileData = fs.statSync(path) // 后续代码被阻塞 不会继续执行

//异步读取 获取到结果后回调执行
fs.stat(filepath,(err,info)=>{
if(err){
console.log(err)
return false
}
console.log(info)
})

//promise方式 代码不会被阻塞
fs.promises.stat(filepath).then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})

//文件描述符
//返回描述符
fs.open(filepath,(err,fd //文件描述符(数字)可以通过这个描述符获取文件信息)=>{
if(err)=>{
console.log(err)
return false
}
//通过描述符获取文件信息 fs.fstat
fs.fstat(fd,(err,info)=>{
console.log(info//文件信息)
console.log(info.isDirectory()//判断是否是文件夹)
})
})

//文件读写
const str = '123'
options有两个选项'
flag:
w打开文件写入 默认值
w+打开文件进行读写 如果不存在则创建并写入
r+打开文件进行1读写,如果不存在则抛出异常
r打开文件读取,读取时默认值
a打开要写入的文件,将流放在文件末尾,如果不存在则创建文件
a+打开文件以进行读写,将流放在末尾,如果不存在则创建文件
fs.writeFile('/1.txt',content,{flag:'a',encoding:''},(err)=>{
if(err){
console.log(err)
}
})

//文件读取 如果不填写encoding,呢嘛返回buffer
fs.readFile('',{encoding:'utf-8'},(err,data)=>{
console.log(data) //buffer二进制在不使用encoding的情况下
})

//文件夹操作
1:创建文件夹
const dirname = './why'
if(fs.existsSync(dirname) //检测文件夹是否存在){
fs.mkdir(dirname,err => {
console.log(err)
})
}
fs.mkdir //盲猜可以弄个脚本自动生成组件文件夹名
2:读取文件夹中所有文件
const readdir(dirname,(err,files)=>{
console.log(files)//获取所有文件
})
//递归调用读取文件名
const getFiles = (dirname)=>{
fs.readdir(firname,{withFileTypes:true},(err,files)=>{
console.log(files)
file.forEach(item=>{
if(item.isDirectory()){
const filepath = path.resolve(dirname,item.name)
getFiles(filepath)
}else{
console.log(item.name)
}
})
})
}
getFiles(dirname)
3:文件夹重命名
fs.rename(旧名称,新名称,err=>{
console.log(err)
})
fs.rename('./why','./kobe',err=>{
console.log(err)
})

events:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//发出事件与监听事件
const EventEmitter = require('event')
const emitter = new EventEmitter()

//监听
//emitter.addEventLister //addEventListener是on的简写
emitter.on('click',(args)=>{
console.log('监听到了click事件',args)
})

//发出
emitter.emit('click',"hao")

//关闭
emitter.off('事件名')

//获取信息
获取注册的事件
console.log(emitter.eventNames())
console.log(emitter.listenerCount('click')) //获取数量
console.log(emitter.listeners('click'))
emitter.once // 只监听一次
emitter.prependListener //将本次放在最前面
emitter.removeAllListeners() //移除所有事件

未完待续……