查看Node版本
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’)
<a name="lUQZC"></a>
### Buffer常用函数
<a name="yAVxv"></a>
#### byteLength
```javascript
const { Buffer } = require('buffer')
// 判断字节长度
const len = Buffer.byteLength('test')
console.log(len);
isBuffer
const { Buffer } = require('buffer')
// 判断是否是buffer对象
let person = {}
console.log(Buffer.isBuffer(person));
concat
const { Buffer } = require('buffer')
// buffer 对象拼接
const buf1 = Buffer.from('This is')
const buf2 = Buffer.from('a')
const buf3 = Buffer.from('monster boy')
const newBufStr = Buffer.concat([buf1,buf2, buf3])
console.log(newBufStr.toString());
Events 模块
Fs 模块
读取文件
readFile(‘文件路径’, ‘编码’, callback(err, dataStr))
const { readFile } = require('fs')
readFile('./demo1.txt', 'utf8', function (err, dataStr){
if (err) return `文件读取失败! ${err.message}`
return dataStr
})
写入文件
writeFile( 文件路径,写入的内容,回调函数 )
// 写入文件
writeFile('./study.txt', 'abcd', function (err) {
// 文件写入成功,返回null
console.log(err);
})
注意:
writeFile 每次写入的数据会替换旧数据,不会创建写入目录,只会写入创建文件
路径
__dirname 获取当前文件所处的绝对路径,常用于路径拼接
// 获取当前文件所处的绝对路径
const filePath = `${__dirname}/files.txt`
console.log(filePath);
Path模块
join
拼接路径
const path = require('path')
// 获取当前目录所在位置的文件
path.join(__dirname, 'study.txt')
// 获取当前文件所在目录的上一级目录的文件路径
path.join(__dirname, '..', 'controller', 'user.js')
// 获取当前文件所在目录的上两级目录的文件路径
path.join(__dirname, '..', '..', 'controller', 'user.js')
basename
获取文件名
const path = require('path')
const str = '/a/b/c/index.html'
// 获取文件名
let re = path.basename(str)
console.log(re); // index.html
// 去除扩展名
let re1 = path.basename(str, '.html')
console.log(re1); // index
extname
获取文件扩展名
const path = require('path')
let str = 'a/b/c/index.html'
// 获取文件扩展名
let extName = path.extname(str)
console.log(extName); // .html
模块
nodejs 中的模块遵循 CommonJS 模块化规范
- 每个模块内部,module 代表当前模块
- module 是一个对象,它的 exports 属性是对外的接口
- 加载某个模块,其实是加载了 module.exports 属性
require
导入模块,require 一个模块时,得到的永远是 module.exports 所指向的对象const user = require('./user')
module
向外共享属性和方法 ```javascript // 模块内变量,外部无法访问 const username = ‘张三’ const age = 18
// 通过 exports 将变量共享给其他模块 module.exports = { username, age }
<a name="GCChH"></a>
### exports
exports 是 module.exports 的简写,它们指向的是同一个地址,因此最终导出结果会以 **module.exports 为准**<br />**export === module.exports**
<a name="NX4el"></a>
## npm
<a name="aE2TX"></a>
### 创建包
快速创建**package.json**包管理文件
```git
npm init -y
包的版本
包的版本是以点分十进制形式定义的
例如:2.24.0
第一位数字:大版本(重构底层后发布的版本)
第二位数字:功能版本(新增了一些功能,底层无变化)
第三位数组:Bug 修复版本
包结构
初次下载包后,会多出一个 node_modules 目录 和 package-lock.json 文件
node_modules 存放所有已安装的包
package-lock.json 记录每一个包下载的信息
nrm
解决下包慢的问题
# 通过 npm 包管理器,将 nrm 安装为全局可用的工具
npm i nrm -g
# 查看所有可用的镜像源
nrm ls
# 将下包的镜像源切换为 taobao 镜像
nrm use taobao
# 查看当前源
nrm current
# 切换源
nrm use <registry>
nrm use taobao
# 添加源
nrm add <registry> <url>
nrm add cpm http://xxxxx
# 删除源
nrm del <registry>
nrm del taobao
# 测试速度
nrm test <registry>
nrm test taobao
全局包
全局包存放路径
C:\Users\用户目录\AppData\Roaming\npm\node_modules
# 安装全局包
npm i 包名 -g
# 卸载全局包
npm uninstall 包名 -g
# 查看全局包
npm list -g --depth 0
基础命令
# 设置淘宝镜像
npm config set registry https://registry.npmmirror.com/
# 查看当前npm镜像源
npm config get registry
# 下载包
npm i moment
# 指定包版本号
npm i moment@2.22.2
# 一次性安装多个包
npm i jquery art-template momnet
# 将包记录到dependencies中
npm i moment -S
# 将包记录到 devDependencies 中
npm i moment -D
# 卸载包
npm uninstall moment