|
|
|
|
# nodejs
|
|
|
|
|
|
|
|
|
|
> 尚硅谷 超哥
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
console.log(arguments)//打印回调函数中的参数
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
进程和线程
|
|
|
|
|
|
|
|
|
|
进程
|
|
|
|
|
|
|
|
|
|
负责为程序运行提供必备的环境
|
|
|
|
|
|
|
|
|
|
相当于工厂中的车间
|
|
|
|
|
|
|
|
|
|
线程
|
|
|
|
|
|
|
|
|
|
计算机中的最小单位,负责执行进程中的任务
|
|
|
|
|
|
|
|
|
|
工厂中的工人
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# nodejs简介
|
|
|
|
|
|
|
|
|
|
* 跨平台,运行于服务器的js运行环境
|
|
|
|
|
* V8引擎,事件驱动,非阻塞,异步IO模型
|
|
|
|
|
* 单线程,可用分布式拓展
|
|
|
|
|
* 开源
|
|
|
|
|
|
|
|
|
|
## 使用Node
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
node file.js
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
es-checker:查看es支持度,可以全局安装
|
|
|
|
|
|
|
|
|
|
## 一 基础
|
|
|
|
|
|
|
|
|
|
### 1.1 模块
|
|
|
|
|
|
|
|
|
|
缺陷:
|
|
|
|
|
|
|
|
|
|
* 没有模块系统
|
|
|
|
|
|
|
|
|
|
* 标准库较少
|
|
|
|
|
|
|
|
|
|
* 没有标准接口
|
|
|
|
|
|
|
|
|
|
* 缺乏管理系统
|
|
|
|
|
|
|
|
|
|
**node.js不能使用import**
|
|
|
|
|
|
|
|
|
|
nodejs的模块实在一个函数中,不是全局作用域
|
|
|
|
|
```js
|
|
|
|
|
//单个
|
|
|
|
|
//模块文件
|
|
|
|
|
//testModule.js
|
|
|
|
|
|
|
|
|
|
function a() {
|
|
|
|
|
// body...
|
|
|
|
|
}
|
|
|
|
|
module.exports=a;
|
|
|
|
|
|
|
|
|
|
//引入文件
|
|
|
|
|
//main.js
|
|
|
|
|
let testModule=require('./testModel');//可以不用加后缀,但是前面的./必须
|
|
|
|
|
testModule();
|
|
|
|
|
|
|
|
|
|
// 多个1
|
|
|
|
|
//模块文件
|
|
|
|
|
//testModule.js
|
|
|
|
|
var func1 = function() {
|
|
|
|
|
console.log("func1");
|
|
|
|
|
};
|
|
|
|
|
var func2 = function() {
|
|
|
|
|
console.log("func2");
|
|
|
|
|
};
|
|
|
|
|
exports.function1 = func1;
|
|
|
|
|
exports.function2 = func2;
|
|
|
|
|
|
|
|
|
|
//引入文件
|
|
|
|
|
//main.js
|
|
|
|
|
let testModule=require('./testModel');//可以不用加后缀,但是前面的./必须
|
|
|
|
|
testModule.function1();
|
|
|
|
|
testModule.function2();
|
|
|
|
|
|
|
|
|
|
// 多个2
|
|
|
|
|
//模块文件
|
|
|
|
|
//testModule.js
|
|
|
|
|
let a=function() {
|
|
|
|
|
console.log("a");
|
|
|
|
|
}
|
|
|
|
|
let b=function() {
|
|
|
|
|
console.log("b");
|
|
|
|
|
}
|
|
|
|
|
module.exports={
|
|
|
|
|
a,
|
|
|
|
|
b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let testModule=require('./testModel')
|
|
|
|
|
testModule.a();
|
|
|
|
|
testModule.b()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 基本内置模块
|
|
|
|
|
### os模块
|
|
|
|
|
//模块都需要导入包
|
|
|
|
|
let os=require("os");
|
|
|
|
|
```js
|
|
|
|
|
let os=require('os');
|
|
|
|
|
hostname();//主机名
|
|
|
|
|
type();//系统名
|
|
|
|
|
platform();//编译时系统名
|
|
|
|
|
arch();//系统架构
|
|
|
|
|
release();//发行版本
|
|
|
|
|
uptime();//操作系统运行时间,单位为秒
|
|
|
|
|
loadavg();//返回一个1,5,15分钟平均负载的数组
|
|
|
|
|
totalmem();//返回系统总内存
|
|
|
|
|
freemem();//空闲内存
|
|
|
|
|
cpus();//数组,每个cpu信息
|
|
|
|
|
networkInterfaces();//获取网络接口列表
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
### url模块
|
|
|
|
|
```js
|
|
|
|
|
let url=require("url")
|
|
|
|
|
|
|
|
|
|
console.log("将url字符串转化为url对象");
|
|
|
|
|
url.parse();
|
|
|
|
|
console.log("将url对象转化为url字符串");
|
|
|
|
|
url.format(urlObj);
|
|
|
|
|
console.log("组合变数,构造url字符串");
|
|
|
|
|
url.resolve(fron,to);
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Query String模块
|
|
|
|
|
|
|
|
|
|
### util模块
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
全局对象
|
|
|
|
|
|
|
|
|
|
**global**
|
|
|
|
|
|
|
|
|
|
包
|
|
|
|
|
|
|
|
|
|
package
|
|
|
|
|
|
|
|
|
|
-package.json 描述文件(必须)
|
|
|
|
|
|
|
|
|
|
-bin 可执行二进制文件
|
|
|
|
|
|
|
|
|
|
-lib js文件
|
|
|
|
|
|
|
|
|
|
-doc 文档
|
|
|
|
|
|
|
|
|
|
-test 单元测试
|
|
|
|
|
|
|
|
|
|
不能写注释
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
npm命令
|
|
|
|
|
|
|
|
|
|
- npm -v 查看npm版本
|
|
|
|
|
- npm version 查看所有模块版本
|
|
|
|
|
- npm search 包名 搜索包
|
|
|
|
|
- npm install/i 包名 安装包
|
|
|
|
|
- npm remove/r 包名 删除包 或者uninstall
|
|
|
|
|
- npm install --save/-S 安装包并添加到依赖
|
|
|
|
|
- npm install 下载当前项目所有依赖
|
|
|
|
|
- npm install -g 安装到全局(一般都是工具)
|
|
|
|
|
-
|
|
|
|
|
- npm list 查看已经按照的包
|
|
|
|
|
|
|
|
|
|
初始化项目 npm init
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
npm国内镜像
|
|
|
|
|
|
|
|
|
|
npm i cnpm -g
|
|
|
|
|
|
|
|
|
|
导包本级找不到,会向上继续找,一直到根目录,如果找不到,报错
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Buffer(缓冲区)
|
|
|
|
|
|
|
|
|
|
-Buffer 的结构和数组很像,操作方式也很像
|
|
|
|
|
|
|
|
|
|
原生数组,性能比较差
|
|
|
|
|
|
|
|
|
|
原生数组存不了2进制文件
|
|
|
|
|
|
|
|
|
|
核心对象,不用导入
|
|
|
|
|
|
|
|
|
|
存储的是2进制,显示为16进制
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
let str= "Hello World!";
|
|
|
|
|
let buf = Buffer.form(str);
|
|
|
|
|
console.log(buf);//存储的是二进制
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`let a = Buffer.alloc(x)`创建定长Buffer
|
|
|
|
|
|
|
|
|
|
`let b = Buffer.allocUnsafe(x)创建不初始化的空间的Buffer,但是含有敏感数据,但是效率高,因为省略了初始化控件的步骤`
|
|
|
|
|
|
|
|
|
|
buffer创建后大小不可更改,否则会出现内存错误,因为buffer在内存上操作
|
|
|
|
|
|
|
|
|
|
给buffer中元素操作,不能超过256,否则会溢出
|
|
|
|
|
|
|
|
|
|
.toString将Buffer转化为字符串
|
|
|
|
|
|
|
|
|
|
存到缓冲区`Buffer.from(str)`
|
|
|
|
|
|
|
|
|
|
### 文件系统fs
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const fs = require("fs");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
同步文件操作和一部文件操作
|
|
|
|
|
|
|
|
|
|
同步会阻塞程序运行,必须等待文件行为完成
|
|
|
|
|
|
|
|
|
|
异步是等操作完成时通过回调函数返回内容
|
|
|
|
|
|
|
|
|
|
* 文件写入(同步) 出现一场会完蛋
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
//打开文件
|
|
|
|
|
fs.openSync(path,flags[,mode])
|
|
|
|
|
- path文件路径
|
|
|
|
|
- flags操作类型
|
|
|
|
|
r只读
|
|
|
|
|
w写入
|
|
|
|
|
-mode(可选),设置文件操作权限,一般不写
|
|
|
|
|
//该方法会返回一个文件对象(文件描述符)
|
|
|
|
|
//let fd =fs.openSync("hellow.txt","w")
|
|
|
|
|
|
|
|
|
|
//写入文件
|
|
|
|
|
fs.writeSync(fd(文件描述符),string[,position(写入起始位置)[,encoding]])
|
|
|
|
|
position起始位置
|
|
|
|
|
encoding写入编码,默认utf-8
|
|
|
|
|
//fs.writeSync(fd,"Hello World!");
|
|
|
|
|
//保存关闭文件
|
|
|
|
|
fs.closeSync(fd);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* 文件写入(异步)
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
//打开文件
|
|
|
|
|
fs.open(path[, flags[, mode]], callback),结果是回调函数
|
|
|
|
|
path <string> | <Buffer> | <URL>
|
|
|
|
|
flags <string> | <number> 请参阅对文件系统 flags 的支持。 默认值: 'r'。
|
|
|
|
|
mode <string> | <integer> 默认值: 0o666 (可读可写)
|
|
|
|
|
callback <Function>
|
|
|
|
|
err <Error>
|
|
|
|
|
fd <integer>
|
|
|
|
|
//fs.open("filename","w",(err,fd)=>{})
|
|
|
|
|
|
|
|
|
|
//写入文件
|
|
|
|
|
fs.write(fd, buffer[, offset[, length[, position]]], callback)//在回调函数中执行
|
|
|
|
|
|
|
|
|
|
//关闭文件
|
|
|
|
|
fs.close(fd[, callback])
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* 简单文件写入
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
fs.writeFile(fs.writeFile(file, data[, options], callback))
|
|
|
|
|
fs.writeFileSync(file, data[, options])
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* 流文件写入(更好印度贵大文件和性能解决方案)
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let fd = fs.createWriteStream(path[,option])
|
|
|
|
|
fd.once("open",()=>{console.log("监听打开事件,因为只会生效一次,所以")})
|
|
|
|
|
fd.wirte(Str)
|
|
|
|
|
fd.close()//不能使用这个,否则会瞬间关闭,后面的写入会失效
|
|
|
|
|
fd.end();//这个可以正常关闭
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
* 读取性质的参考api
|