目录
1.path模块
const path = require('path')
1.1 normalize
将字符串处理成路径
const nor = path.normalize('a/b/c///d\\\w')
console.log(nor)
// a\b\c\d\w
1.2 join
合并路径
const join = path.join('/wq/w', 'we///w')
console.log(join)
// \wq\w\we\w
1.3 resolve
将路径返回为绝对路径
const resolve = path.resolve('/s/sd')
console.log(resolve)
// D:\s\sd
1.4 extname
返回该路径下的文件名的后缀
const ext = path.extname('/a/s/c.txt')
console.log(ext)
// .txt
1.5 basename
返回该路径下的文件名
const basename = path.basename('/a/s/c.txt')
console.log(basename)
// c.txt
1.6 dirname
返回当前目录
const dir = path.dirname('/a/s/c.txt')
console.log(dir)
// /a/s
1.7 parse
将路径进行解析
- root
- dir
- base
- ext
- name
const par = path.parse('/a/s/c.txt')
console.log(par)
// { root: '/', dir: '/a/s', base: 'c.txt', ext: '.txt', name: 'c' }
1.8 format
与parse相反,将对象解析成路径
const format = path.format({ root: '/', dir: '/a/s', base: 'c.txt', ext: '.txt', name: 'c' })
console.log(format)
// /a/s\c.txt
2.fs模块
const fs = require('fs')
2.1 readFile
文件读取
fs.readFile('./a.txt', (err, data) => {
if (err) {
return console.error(err)
}
console.log(data.toString())
})
2.2 writeFile
文件写入,不管写入成功还是写入失败都会执行后面的回调函数
fs.writeFile('./a.txt', 'asdasd', (err) => {
if (err) {
return console.error(err)
}
console.log('写入成功')
})
3.buffer
- buffer用来处理二进制数据流
- buffer实例类似数组,大小是固定的
- buffer是使用c++代码在v8堆外分配的物理内存
- buffer是一个全局变量
const { Buffer } = require('buffer');
// 创建长度为 10 的以零填充的缓冲区。
const buf1 = Buffer.alloc(10);
// 创建长度为 10 的缓冲区,
// 使用值为 `1` 的字节填充。
const buf2 = Buffer.alloc(10, 1);
// 创建长度为 10 的未初始化的缓冲区。
// 这比调用 Buffer.alloc() 快,
// 但返回的缓冲区实例可能包含旧数据,
// 需要使用 fill()、write() 、
// 或其他填充缓冲区内容的函数重写。
const buf3 = Buffer.allocUnsafe(10);
// 创建包含字节 [1, 2, 3] 的缓冲区。
const buf4 = Buffer.from([1, 2, 3]);
// 创建包含字节 [1, 1, 1, 1] 的缓冲区,
// 所有条目都使用 `(value & 255)` 截断以符合范围 0–255。
const buf5 = Buffer.from([257, 257.5, -255, '1']);
// 创建包含字符串 'tést' 的 UTF-8 编码字节的缓冲区:
// [0x74, 0xc3, 0xa9, 0x73, 0x74](十六进制)
// [116, 195, 169, 115, 116](十进制)
const buf6 = Buffer.from('tést');
// 创建包含 Latin-1 字节 [0x74, 0xe9, 0x73, 0x74] 的缓冲区。
const buf7 = Buffer.from('tést', 'latin1');
常用类:
http://nodejs.cn/api/buffer.html#buffer_class_buffer
4.events
事件触发器
const EventEmitter = require('events')
class CustEvent extends EventEmitter {}
const ce = new CustEvent()
添加一个事件
ce.on('test', () => {
console.log('this is a test')
})
执行该事件
ce.emit('test')
移除该事件
ce.removeAllListener('test')
5.http模块
http模块可搭建web服务器
const http = require('http')
const app = http.createServer((req, res) => {
res.write('1111')
res.end()
})
app.listen(3000, () => {
console.log('服务器已启动')
})
5.1 get请求
const http = require('http')
const url = require('url')
const app = http.createServer((req, res) => {
// 解析请求路径
// 如果parse第二个参数不穿true则不解析成对象
console.log(url.parse(req.url, true))
res.end()
})
app.listen(3000, () => {
console.log('服务器已启动')
})
当请求路径为
http://localhost:3000/sta/a?id=1&username=tyh
解析后:
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?id=1&username=tyh',
query: [Object: null prototype] { id: '1', username: 'tyh' },
pathname: '/sta/a',
path: '/sta/a?id=1&username=tyh',
href: '/sta/a?id=1&username=tyh'
}
5.2 post请求
const http = require('http')
// const url = require('url')
const querystring = require('querystring')
const app = http.createServer((req, res) => {
const path = req.url
console.log(path)
// console.log(url.parse(req.url, true))
if (path === '/reg') {
let body = ''
req.on('data', (chunk) => {
body += chunk
console.log(body)
})
req.on('end', () => {
body = querystring.parse(body)
console.log(body)
})
}
res.end()
})
app.listen(3000, () => {
console.log('服务器已启动')
})