查看Node版本

  1. node -v

Buffer 模块

什么是Buffer ?

  • buffer 是用来处理二进制数据流
  • buffer 类似数组,大小是固定的
  • buffer 是一个全局变量
  • 虽然 Buffer 类在全局作用域内可用,但仍然建议通过 import 或 require 语句显式地引用它

    创建Buffer的方式

    ```javascript const { Buffer } = require(‘buffer’)

// 创建长度为 10 的以零填充的缓冲区。 const buf1 = Buffer.alloc(10) //

// 创建长度为 10 的缓冲区, // 使用值为 1 的字节填充。 const buf2 = Buffer.alloc(10,1) //

// 创建包含字节 [1, 2, 3] 的缓冲区。 const buf3 = Buffer.from([1,2,3]) //

// 创建包含字符串 ‘tést’ 的 UTF-8 编码字节的缓冲区: const buf4 = Buffer.from(‘test’)

// 创建包含 base64 字节 ‘tést’ 的的缓冲区: const buf5 = Buffer.from(‘text’, ‘base64’)

  1. <a name="lUQZC"></a>
  2. ### Buffer常用函数
  3. <a name="yAVxv"></a>
  4. #### byteLength
  5. ```javascript
  6. const { Buffer } = require('buffer')
  7. // 判断字节长度
  8. const len = Buffer.byteLength('test')
  9. console.log(len);

isBuffer

  1. const { Buffer } = require('buffer')
  2. // 判断是否是buffer对象
  3. let person = {}
  4. console.log(Buffer.isBuffer(person));

concat

  1. const { Buffer } = require('buffer')
  2. // buffer 对象拼接
  3. const buf1 = Buffer.from('This is')
  4. const buf2 = Buffer.from('a')
  5. const buf3 = Buffer.from('monster boy')
  6. const newBufStr = Buffer.concat([buf1,buf2, buf3])
  7. console.log(newBufStr.toString());

Events 模块

pass

Fs 模块

读取文件

readFile(‘文件路径’, ‘编码’, callback(err, dataStr))

  1. const { readFile } = require('fs')
  2. readFile('./demo1.txt', 'utf8', function (err, dataStr){
  3. if (err) return `文件读取失败! ${err.message}`
  4. return dataStr
  5. })

写入文件

writeFile( 文件路径,写入的内容,回调函数 )

  1. // 写入文件
  2. writeFile('./study.txt', 'abcd', function (err) {
  3. // 文件写入成功,返回null
  4. console.log(err);
  5. })

注意:
writeFile 每次写入的数据会替换旧数据,不会创建写入目录,只会写入创建文件

路径

__dirname 获取当前文件所处的绝对路径,常用于路径拼接

  1. // 获取当前文件所处的绝对路径
  2. const filePath = `${__dirname}/files.txt`
  3. console.log(filePath);

Path模块

join

拼接路径

  1. const path = require('path')
  2. // 获取当前目录所在位置的文件
  3. path.join(__dirname, 'study.txt')
  4. // 获取当前文件所在目录的上一级目录的文件路径
  5. path.join(__dirname, '..', 'controller', 'user.js')
  6. // 获取当前文件所在目录的上两级目录的文件路径
  7. path.join(__dirname, '..', '..', 'controller', 'user.js')

basename

获取文件名

  1. const path = require('path')
  2. const str = '/a/b/c/index.html'
  3. // 获取文件名
  4. let re = path.basename(str)
  5. console.log(re); // index.html
  6. // 去除扩展名
  7. let re1 = path.basename(str, '.html')
  8. console.log(re1); // index

extname

获取文件扩展名

  1. const path = require('path')
  2. let str = 'a/b/c/index.html'
  3. // 获取文件扩展名
  4. let extName = path.extname(str)
  5. console.log(extName); // .html

模块

nodejs 中的模块遵循 CommonJS 模块化规范

  1. 每个模块内部,module 代表当前模块
  2. module 是一个对象,它的 exports 属性是对外的接口
  3. 加载某个模块,其实是加载了 module.exports 属性

    require

    导入模块,require 一个模块时,得到的永远是 module.exports 所指向的对象
    1. const user = require('./user')

    module

    向外共享属性和方法 ```javascript // 模块内变量,外部无法访问 const username = ‘张三’ const age = 18

// 通过 exports 将变量共享给其他模块 module.exports = { username, age }

  1. <a name="GCChH"></a>
  2. ### exports
  3. exports 是 module.exports 的简写,它们指向的是同一个地址,因此最终导出结果会以 **module.exports 为准**<br />**export === module.exports**
  4. <a name="NX4el"></a>
  5. ## npm
  6. <a name="aE2TX"></a>
  7. ### 创建包
  8. 快速创建**package.json**包管理文件
  9. ```git
  10. npm init -y

包的版本

包的版本是以点分十进制形式定义的
例如:2.24.0
第一位数字:大版本(重构底层后发布的版本)
第二位数字:功能版本(新增了一些功能,底层无变化)
第三位数组:Bug 修复版本

包结构

初次下载包后,会多出一个 node_modules 目录 和 package-lock.json 文件
node_modules 存放所有已安装的包
package-lock.json 记录每一个包下载的信息

nrm

解决下包慢的问题

  1. # 通过 npm 包管理器,将 nrm 安装为全局可用的工具
  2. npm i nrm -g
  3. # 查看所有可用的镜像源
  4. nrm ls
  5. # 将下包的镜像源切换为 taobao 镜像
  6. nrm use taobao
  7. # 查看当前源
  8. nrm current
  9. # 切换源
  10. nrm use <registry>
  11. nrm use taobao
  12. # 添加源
  13. nrm add <registry> <url>
  14. nrm add cpm http://xxxxx
  15. # 删除源
  16. nrm del <registry>
  17. nrm del taobao
  18. # 测试速度
  19. nrm test <registry>
  20. nrm test taobao

全局包

全局包存放路径
C:\Users\用户目录\AppData\Roaming\npm\node_modules

  1. # 安装全局包
  2. npm i 包名 -g
  3. # 卸载全局包
  4. npm uninstall 包名 -g
  5. # 查看全局包
  6. npm list -g --depth 0

基础命令

  1. # 设置淘宝镜像
  2. npm config set registry https://registry.npmmirror.com/
  3. # 查看当前npm镜像源
  4. npm config get registry
  5. # 下载包
  6. npm i moment
  7. # 指定包版本号
  8. npm i moment@2.22.2
  9. # 一次性安装多个包
  10. npm i jquery art-template momnet
  11. # 将包记录到dependencies中
  12. npm i moment -S
  13. # 将包记录到 devDependencies 中
  14. npm i moment -D
  15. # 卸载包
  16. npm uninstall moment