Node.js(二)

194 阅读5分钟

核心模块

核心模块path

  1. 内置模块,require之后直接使用,主要作用:用于处理文件/目录的路径
  2. basename() 获取路径中基础名称
  3. dirname() 获取路径中目录名称
  4. extname() 获取路径中扩展名称
  5. isAbsolute() 获取路径是否为绝对路径
  6. join() 拼接多个路径
  7. resolve() 返回绝对路径
  8. parse() 解析路径
  9. format() 序列化路径
  10. normalize() 规范化路径
const path = require('path') // 引入 node.js 的 path 模块

// __filename /Users/xujingquan/Desktop/learn-code/node-learn/核心模块/1-path.js
console.log('__filename', __filename) // 输出完整的路径

// 1 ---- 获取路径中的基础名称
/**
 * 01 返回的就是接收路径的最后一个部分
 * 02 参数的类型需要是一个字符串
 * 03 第二个参数表示扩展名,如果说没有设置则返回完整的文件名称带后缀
 * 04 第二个参数做为后缀时,如果没有在当前路径中被匹配,那么就会忽略
 * 05 处理目录路径的时候如果说结尾处有路径分割符,则也会被忽略掉
 */
// 输出 1-path.js
console.log(path.basename(__filename))
// 还可以接收第二个参数,是可选的
// 输出 1-path,如果传.css 的话就会输出 1-path.js
console.log(path.basename(__filename, '.js'))
// 下面两个都是返回c  返回的就是pat路径的最后一个部分
console.log(path.basename('/a/b/c'))
console.log(path.basename('/a/b/c/'))

// 2 --- 获取路径目录名

/**
 * 01 返回路径中最后一个部分的上一层目录所在路径
 * 02 处理目录路径的时候如果说结尾处有路径分割符,则也会被忽略掉
 */
// /Users/xujingquan/Desktop/learn-code/node-learn/核心模块
console.log(path.dirname(__filename))
//  下面两个都是返回 /a/b
console.log(path.dirname('/a/b/c'))
console.log(path.dirname('/a/b/c/'))

// 3 --- 获取路径的扩展名
/**
 * 01 返回 path 路径中相对应的后缀名
 * 02 如果 path 路径中存在多个点,它匹配的是最后一个点,到结尾的内容
 */
// .js
console.log(path.extname(__filename))
// 返回空
console.log(path.extname('/a/b/c'))
// .css
console.log(path.extname('/a/b/.html.js.css'))

// 4 --- 解析路径
/**
 * 01 接收一个路径,返回一个对象,包含不同的信息
 * 02 root dir base ext name
 */
const obj = path.parse('/a/b/c/index.html') //文件路径
// {
//   root: '/',  根路径
//   dir: '/a/b/c',
//   base: 'index.html',完整文件名称
//   ext: '.html', 文件后缀
//   name: 'index' 文件名
// }
console.log(obj)

const obj1 = path.parse('/a/b/c') // 目录路径
// {
//   root: '/',  根路径
//   dir: '/a/b',
//   base: 'c',完整文件名称
//   ext: '', 文件后缀
//   name: 'c' 文件名
// }
console.log(obj1)

// 5 --- 序列化路径
const obj2 = path.parse('./a/b/c/')
// ./a/b/c
console.log(path.format(obj2))

// 6 --- 判断当前路径是否为绝对路径
console.log(path.isAbsolute('foo')) // false
console.log(path.isAbsolute('/foo')) // true
console.log(path.isAbsolute('////foo')) // true
console.log(path.isAbsolute('')) // false
console.log(path.isAbsolute('.')) // false
console.log(path.isAbsolute('../bar')) // false

// 7 --- 拼接路径
// a\b\c\index.html
console.log(path.join('a/b', 'c', 'index.html'))
// \a\b\c\index.html
console.log(path.join('/a/b', 'c', 'index.html'))
// \a\b\index.html      ../ 是返回上级目录
console.log(path.join('/a/b', 'c', '../', 'index.html'))
// \a\b\c\index.html     ./是当前目前或者同级目录
console.log(path.join('/a/b', 'c', './', 'index.html'))
// \a\b\c\index.html
console.log(path.join('/a/b', 'c', '', 'index.html'))
// .
console.log(path.join(''))

// 8 --- 规范化路径
// .
console.log(path.normalize(''))
// a/b/c/d
console.log(path.normalize('a/b/c/d'))
// a/b/c../d
console.log(path.normalize('a//b/c../d'))
// a/b/c/d
console.log(path.normalize('a//\\/b/c\\/d'))
// a/c/d    \b 被转译了
console.log(path.normalize('a//\\b/c\\/d'))

// 9 --- 绝对路径
/**
 * resolve([from],to)
 */
// /Users/xujingquan/Desktop/learn-code/node-learn/核心模块
console.log(path.resolve())
// /Users/xujingquan/Desktop/learn-code/node-learn/核心模块/a/b
console.log(path.resolve('a', 'b'))
// /b
console.log(path.resolve('a', '/b'))
// /b
console.log(path.resolve('/a', '/b'))
// /a/b
console.log(path.resolve('/a', 'b'))
//  /Users/xujingquan/Desktop/learn-code/node-learn/核心模块/index.html
console.log(path.resolve('index.html'))

全局变量之Buffer

  1. 无须 require 的一个全局变量
  2. 实现 Nodejs 平台下的二进制数据操作
  3. 不占据 V8 堆内存大小的内存空间
  4. 内存的使用由Node来控制,由v8的GC回收
  5. 一般配合Stream 流使用,充当数据缓冲区

创建Buffer

  1. 创建 Buffer 实例
  2. ·alloc:创建指定字节大小的 buffer
  3. ·allocUnsafe:创建指定大小的buffer(不安全)
  4. ·from:接收数据,创建buffer
const b1 = Buffer.alloc(10)
console.log(b1) // <Buffer 00 00 00 00 00 00 00 00 00 00>

const b2 = Buffer.allocUnsafe(10)
console.log(b2) // <Buffer 00 00 00 00 00 00 00 00 a0 94> 随机

const b3 = Buffer.from('1', 'utf-8')
const b4 = Buffer.from('哈', 'utf-8')
console.log(b3) // <Buffer 31>
console.log(b4) // <Buffer e5 93 88>

const b5 = Buffer.from([1, 2, 3])
console.log(b5) // <Buffer 01 02 03>

const b6 = Buffer.from([1, 2, '哈'])
console.log(b5) // <Buffer 01 02 03>

const b7 = Buffer.from('哈', 'utf-8')
console.log(b7.toString()) // 哈

const b8 = Buffer.from([0xe5, 0x93, 0x88])
console.log(b8.toString()) // 哈

Buffer实例方法

  1. fill:使用数据填充buffer
  2. write:向buffer中写入数据
  3. toString:从buffer中提取数据
  4. slice:截取buffer
  5. indexOf:在buffer中查找数据
  6. copy:拷贝buffer中的数据
// fill
let buf = Buffer.alloc(6)
buf.fill('123')        // <Buffer 31 32 33 31 32 33>  123123
buf.fill('123456789')  // <Buffer 31 32 33 34 35 36>  123456
buf.fill('123', 1)     // <Buffer 00 31 32 33 31 32>  12312   表示从下标1开始
buf.fill('123', 1, 3)  // <Buffer 00 31 32 00 00 00>  12      第二个参数表示到那个下标结束
buf.fill('123', 1, 2)  // <Buffer 00 31 00 00 00 00>  1
buf.fill(123)          // <Buffer 7b 7b 7b 7b 7b 7b>  {{{{{{
console.log(buf)
console.log(buf.toString())

// write
let buf1 = Buffer.alloc(6)
buf1.write('123')       // <Buffer 31 32 33 00 00 00> 123
buf1.write('123', 1)    // <Buffer 00 31 32 33 00 00> 123 从第一个开始写入
buf1.write('123', 1, 3) // <Buffer 00 31 32 33 00 00> 123 第二个参数表示长度
buf1.write('123', 1, 2) // <Buffer 00 31 32 00 00 00> 12
console.log(buf1)
console.log(buf1.toString())

// toString
let buf2 = Buffer.from('学习学习')
console.log(buf2)                         // <Buffer e5 ad a6 e4 b9 a0>
console.log(buf2.toString('utf-8'))       //  学习学习   utf-8是默认的
console.log(buf2.toString('utf-8', 3))    // 习学习 第二个参数表示从那个下标开始截取,因为一个汉字对应3个字节
console.log(buf2.toString('utf-8', 3, 9)) // 习学 第三个参数标识长度,因为一个汉字对应3个字节(顾头不顾尾)

// slice
let buf3 = Buffer.from('学习学习')
console.log(buf3.slice())     // <Buffer e5 ad a6 e4 b9 a0 e5 ad a6 e4 b9 a0>
console.log(buf3.slice(3))    // <Buffer e4 b9 a0 e5 ad a6 e4 b9 a0>  第一个参数表示从那个下标开始截取,因为一个汉字对应3个字节
console.log(buf3.slice(3, 9)) // <Buffer e4 b9 a0 e5 ad a6> 第三个参数标识长度,因为一个汉字对应3个字节(顾头不顾尾)
console.log(buf3.slice(-3))   // <Buffer e4 b9 a0> 从后往前取

// indexOf
let buf4 = Buffer.from('学习学习,饿abc也是,撒大大')
console.log(buf4.indexOf('饿'))   // 13 中文3个字节
console.log(buf4.indexOf('阿訇')) // -1  不存在返回 -1
console.log(buf4.indexOf('学'))   // 0

// copy
let buf5 = Buffer.alloc(6)
let buf6 = Buffer.from('学习123')
buf6.copy(buf5) // 学习123 学习123 // 将 buf6 里面的数据拷贝到 buf5 里面
buf6.copy(buf5, 3) // 学  学习123  // 第一个参数表示从那里开始写入,中文3个字节
buf6.copy(buf5, 3, 3) // 习  学习123 // 第二个参数表示从哪里开始读取
buf6.copy(buf5, 3, 3, 6) // 习  学习123 // 第三个参数表示从哪里结束读取 (顾头不顾尾)
console.log(buf5.toString())
console.log(buf6.toString())

Buffer静态方法

  1. concat:将多个 buffer 拼接成一个新的 buffer
  2. isBuffer:判断当前数据是否为 buffer
let b1 = Buffer.from('学习')
let b2 = Buffer.from('自学')

console.log(Buffer.concat([b1, b2]).toString())    // 学习自学
console.log(Buffer.concat([b1, b2], 9).toString()) // 学习自

let b1 = Buffer.alloc(3)
let b2 = ''
console.log(Buffer.isBuffer(b1)) // true
console.log(Buffer.isBuffer(b2)) // false