# Path

提供了一些工具函数,用于处理文件与目录的路径

basename、dirname与extname

  • 返回一个 path 的最后一部分
  • 返回一个 path 的目录名
  • 返回 path 的扩展名
const {basename,dirname,extname} = require("path");

const filePath = "/usr/local/bin/test.txt";

console.log(basename(filePath)); // test.txt
console.log(dirname(filePath)); // /usr/local/bin
console.log(extname(filePath)); // .txt

@前端进阶之旅: 代码已经复制到剪贴板

join、normalize与resolve

  • path.join() 方法使用平台特定的分隔符把全部给定的 path 片段连接到一起,并规范化生成的路径
  • path.normalize() 方法会规范化给定的 path,并解析 '..' 和 '.' 片段
  • path.resolve() 方法会把一个相对路径解析为一个绝对路径
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
// 返回: '/foo/bar/baz/asdf'

path.join('foo', {}, 'bar');
// 抛出 'TypeError: Path must be a string. Received {}'
@前端进阶之旅: 代码已经复制到剪贴板

path.format与path.parse

  • path.format() 方法会从一个对象返回一个路径字符串。 与 path.parse() 相反
  • path.parse() 方法返回一个对象,对象的属性表示 path 的元素
const {parse,format} = require("path");

const filePath = "/usr/local/bin/test.txt";

const parse_ret = parse(filePath);
const format_ret = format(parse_ret)

console.log(parse_ret);
console.log(format_ret);
@前端进阶之旅: 代码已经复制到剪贴板
  • 结果
{ root: '/',
  dir: '/usr/local/bin',
  base: 'test.txt',
  ext: '.txt',
  name: 'test' }
/usr/local/bin\test.txt
@前端进阶之旅: 代码已经复制到剪贴板

sep、delimiter、win32、posix

和操作系统有关的东西

  • sep:提供了平台特定的路径片段分隔符
    • Windows 上是 \
    • POSIX 上是 /
  • path.win32提供了 path 方法针对 Windows 的实现
  • posix 属性提供了 path 方法针对 POSIX 的实现
  • delimiter 就是 ":"
const {sep,delimiter,win32,posix} = require("path");

const filePath = "/usr/local/bin/test.txt";


console.log("sep ",sep);
console.log("posix sep ",posix.sep);
console.log("win32 sep ",win32.sep);
console.log("PATH",process.env.PATH);
console.log("delimiter ",delimiter);

@前端进阶之旅: 代码已经复制到剪贴板
sep  \
posix sep  /
win32 sep  \
PATH C:\Python27\;C:\Python27\Scripts;C:\Windows\system32;C:\Windows;C:\Windows\
System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\b
in;C:\Program Files\nodejs;;e:\Program Files\VS Code\bin;C:\Users\Administrator\
AppData\Roaming\npm;.;;.;;.;
delimiter  ;
@前端进阶之旅: 代码已经复制到剪贴板

回顾小结

const path = require("path")

// 路径相关
console.log("__dirname", __dirname); //文件夹
console.log("__filename", __filename); //文件名
console.log("process.cwd()", process.cwd()); //绝对路径
console.log("./", path.resolve()); //根据相对路径取得绝对路径
@前端进阶之旅: 代码已经复制到剪贴板
  • __dirname、__filename 总是返回文件的绝对路径
  • process.cwd()总是返回node命令所在文件夹(就是用户在哪里启动的node脚本的路径)

# Buffer

  • buffer用来处理二进制数据流的
  • 类似于整数数组,大小固定

Buffer 类在 Node.js 中是一个全局变量,因此无需使用 require(‘buffer’).Buffer

// 创建一个长度为 10、且用 0 填充的 Buffer。
const buf1 = Buffer.alloc(10)

// 创建一个长度为 10、且用 0x1 填充的 Buffer。 
const buf2 = Buffer.alloc(10, 1);

// 创建一个包含 [0x1, 0x2, 0x3] 的 Buffer。
const buf4 = Buffer.from([1, 2, 3]);
@前端进阶之旅: 代码已经复制到剪贴板

Buffer类常用的方法

  • byteLength 计算字符串占了几个字节
  • Buffer.isBuffer判断是否是buffer对象
  • Buffer.concat拼接Buffer
// byteLength 计算字符串占了几个字节
console.log(Buffer.byteLength("test")) // 4 
console.log(Buffer.byteLength("测试"))  // 6字节

// Buffer.isBuffer判断是否是buffer对象
console.log(Buffer.isBuffer({}))
console.log(Buffer.isBuffer(Buffer.from([1,2,3])))

// Buffer.concat拼接Buffer

const buf1 = Buffer.from("This ")
const buf2 = Buffer.from("is ")
const buf3 = Buffer.from("a ")
const buf4 = Buffer.from("test ")
const buf5 = Buffer.from("buffer")

const buf = Buffer.concat([buf1,buf2,buf3,buf4,buf5]);

console.log(buf.toString());

/**
4
6
false
true
This is a test buffer
**/
@前端进阶之旅: 代码已经复制到剪贴板

Buffer实例常用方法

  • buf.length
  • buf.toString()
  • buf.fill() 填充一些值
  • buf.equals() 两个buffer的内容是否相等
  • buf.indexOf() 找到字符的位置
  • buf.copy()
const buf = Buffer.from("This is a test!");

console.log(buf.length); // 15

console.log(buf.toString("base64")); 

const buf1 = Buffer.allocUnsafe(10);

// buf1.fill填充一些值
//<Buffer a0 b4 25 00 00 00 00 00 01 00>
//<Buffer a0 b4 0a 0a 0a 0a 00 00 01 00>
console.log(buf1)
console.log(buf1.fill(10,2,6)) // 从第二个开始到第六个填充10

// buf.equals两个buffer的内容是否相等

const buf2 = Buffer.from("test")
const buf3 = Buffer.from("test")
const buf4 = Buffer.from("test!")

console.log(buf2.equals(buf3)) // true
console.log(buf2.equals(buf4)) // false


// buf.indexOf() 找到字符的位置

console.log(buf2.indexOf("es")); // 1
console.log(buf2.indexOf("esa")); // -1


// 解决中文乱码问题
const StringDecoder = require("string_decoder"