美文网首页Web
Node.js的学习总结(二)

Node.js的学习总结(二)

作者: 追逐_chase | 来源:发表于2019-12-16 16:08 被阅读0次
node.jpeg

1. Buffer类

1.什么是Buffer?
Buffer是NodeJS全局对象上的一个类, 是一个专门用于存储字节数据的类

1. 创建一个Buffer对象

Buffer.alloc(size[, fill[, encoding]])

let buff = Buffer.alloc(5);
// 注意点: 通过console.log();输出Buffer. 会自动将存储的内容转换成16进制再输出

let buff1 = Buffer.alloc(5,"a");
console.log(buff1);

根据数组/字符串创建一个Buffer对象 ,Buffer.from(string[, encoding])

let buf = Buffer.from([1,3,5]);
console.log(buf);

console.dir(buf);
//打印结果: Buffer [Uint8Array] [ 1, 3, 5 ]

Buffer对象本质:是一个数组

2.Buffer的实例方法
  1. 将二进制数据转换成字符串,buf.toString()
//97,98,99对应的ASR码值
let buf = Buffer.from([97,98,99]);
console.log(buf);
//转化对应的字符换是abc
console.log(buf.toString());

2.往Buffer中写入数据

  • string 要写入 buf 的字符串。
  • offset 开始写入 string 之前要跳过的字节数。默认值: 0。
  • length <integer> 要写入的字节数。默认值: buf.length - offset。
  • encoding <string> string 的字符编码。默认值: 'utf8'。
    buf.write(string[, offset[, length]][, encoding])
//创建一个对象
let buf1 = Buffer.alloc(5);
//写入数据
buf1.write("abcdefg");
// <Buffer 61 62 63 64 65> 
//从打印结果上可以看出 写入了5个数据 abcde,后面的fg就会忽略掉,因为只有5个字节存储
console.log(buf1);

// 向后 偏移2个字节 存储数据
buf1.write("abcdefg",2);

3.从指定位置截取新Buffer

  • start <integer> 新 Buffer 开始的位置。默认值: 0。
  • end <integer> 新 Buffer 结束的位置(不包含)
    buf.slice([start[, end]])
let buf1 = Buffer.from("abcdefg");
let buf2 = buf1.slice(2,4);
console.log(buf2);
console.log(buf2.toString());
3.Buffer的静态方法
  1. Buffer.concat(list[, totalLength]),合并Buffer中的数据
let buf1 = Buffer.from("123");
let buf2 = Buffer.from("abc");
let buf3 = Buffer.from("xxx");
let res = Buffer.concat([buf1, buf2, buf3]);
console.log(res);
console.log(res.toString());
  1. 获取Buffer实际字节长度
    Buffer.byteLength(string[, encoding])
let buf = Buffer.from("123")
 let res = Buffer.byteLength(buf);
console.log(res);

2.Path路径模块

  • 封装了各种路径相关的操作
  • 需要手动投入这个模块
// 导入系统 Path模块
 let path = require("path");

1.获取路径的最后一部分 path.basename(path[, ext])

let res = path.basename("/a/b/c/index.html");
 //打印结果是:index.html
 console.log(res);

 //参数一:路径
 //参数二: 可选的文件扩展名。
 let res1 = path.basename("/a/b/c/index.html",".html");
 //  index
 console.log(res1);

2.获取路径 path.dirname(path)

let res2 = path.dirname("user/location/index.html");
//打印结果: user/location
 console.log(res2);
  1. extname-获取文件的扩展名称
 let res3 = path.extname("user/location/index.html");
 //打印结果: .html
 console.log(res3);

4.将路径转化为对象 path.parse()
path.format()obj->string

 //转化成对象
 let obj = path.parse("/a/b/c/index.html")  ;
 console.log(obj);

/*
 打印结果:
{
  root: '/',
  dir: '/a/b/c',
  base: 'index.html',
  ext: '.html',
  name: 'index'
}
*/

 //转化成字符串
 console.log( path.format(obj));

5.拼接路径 path.join([...paths])

let str = path.join("/a/b", "c"); 
 // 打印结果: /a/b/c

  1. path.normalize(path): 用于规范化路径
let res = path.normalize("/a//b///c////d/////index.html");
console.log(res);
  1. path.resolve([...paths]): 用于解析路径
// let res = path.resolve('/foo/bar', './baz'); // /foo/bar/baz
// let res = path.resolve('/foo/bar', '../baz'); // /foo/baz
let res = path.resolve('/foo/bar', '/baz'); // /baz
console.log(res);
  1. path.relative(from, to): 用于计算相对路径
let res = path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');

console.log(res);
// /data/orandea

相关文章

网友评论

    本文标题:Node.js的学习总结(二)

    本文链接:https://www.haomeiwen.com/subject/fcmzgctx.html