Node
- Node是ES标准的一个实现,Node也是一个JS引擎
- 通过Node可以使js代码在服务端执行
- Node仅仅对ES标准进行了实现,所以在Node中不包含DOM 和 BOM
- Node中可以使用所有的内建对象
String Number Boolean Math Date RegExpFunction Object Array而BOM和DOM都不能使用但是可以使用 console 也可以使用定时器(setTimeout() setInterval()) - Node可以在后台来编写服务器
Node编写服务器都是单线程的服务器
进程:进程就是一个一个的工作计划,为程序的执行提供环境
线程:线程是计算机最小的运算单位,执行程序 - 传统服务器都是多线程的
每一个请求,就创建一个线程去处理请求 - Node的服务器是单线程的
Node处理请求时是单线程的,但是在后台拥有一个I/O线程池
模块化 - ES5中没有原生支持模块化,我们只能通script标签引入js文件来实现模块化
在node中为了对模块管理,引入了CommonJS规范 - 模块的引用
使用 require()函数来引入一个模块
var 变量 = require(“模块的标识”);
模块的定义
- 在node中一个js文件就是一个模块
默认情况下在js文件中编写的内容,都是运行在一个独立的函数中,外部的模块无法访问 - 导出变量和函数
使用 exports
exports.属性 = 属性值;
exports.方法 = 函数;
使用module.exports
module.exports.属性 = 属性值;
module.exports.方法 = 函数;
module.exports = {};
exports.x="xxxxx";
exports.add=function(a,b){
return a+b
};
exports.mul=function (a,b) {
return a*b
};
模块的标识
- 模块的标识就是模块的名字或路径
我们node通过模块的标识来寻找模块的,对于核心模块(npm中下载的模块),直接使用模块的名字对其进行引入
var fs = require(“fs”);
var express = require(“express”);
对于自定义的文件模块,需要通过文件的路径来对模块进行引入路径可以是绝对路径,如果是相对路径必须以./或 …/开头
var router = require("./router");
npm的命令
- npm -v 查看npm的版本
- npm version 查看所有模块的版本
- npm search 包名 搜索包
- npm install / i 包名 安装包
- npm remove / r 包名 删除包
- npm install 包名 --save 安装包并添加到依赖中 *****
- npm install 下载当前项目所依赖的包
- npm install 包名 -g 全局安装包(全局安装的包一般都是一些工具)
文件系统(File System)
- Buffer(缓冲区)
Buffer和数组的结构的非常类似,Buffer是用来存储二进制数据的 - Buffer的方法
Buffer.from(字符串)
将一个字符串中内容保存到一个buffer中
buf.toString()
将buffer转换为一个字符串
Buffer.alloc(size)
创建一个指定大小的buffer对象
Buffer.allocUnsafe(size)
创建一个指定大小的buffer对象,可以包含敏感数据
var str="nihao atguigu"
//将一个字符床转换为Buffer
var buf=Buffer.from(str)
console.log(buf.toString())
//创建一个指定大小的Buffer
var buf1=Buffer.alloc(10)
for (var i=0;i<buf1.length;i++)
{
buf1[i]=i
}
console.log(buf1);
//创建一个指定大小的Buffer,但是可能包含敏感数据
var buf3=Buffer.allocUnsafe(10)
console.log(buf3.toString());
-
fs模块
引入fs模块
var fs = require(“fs”); -
fs模块中的大部分操作都提供了两种方法,同步方法和异步方法
同步方法带sync
异步方法没有sync,都需要回调函数 -
写入文件
1.同步写入
2.异步写入
3.简单写入
4.流式写入 -
读取文件
1.同步读取
2.异步读取
3.简单读取
4.流式读取 -
方法
-
打开文件
fs.open(path, flags[, mode], callback)
fs.openSync(path, flags[, mode]) -
读写文件
fs.write(fd, string[, position[, encoding]], callback)
fs.writeSync(fd, string[, position[, encoding]])
fs.read(fd, buffer, offset, length, position, callback)
fs.readSync(fd, buffer, offset, length, position)
-
关闭文件
fs.close(fd,callback)
fs.closeSync(fd); -
简单文件读取和写入
fs.writeFile(file, data[, options], callback)
fs.writeFileSync(file, data[, options])
fs.readFile(path[, options], callback)
fs.readFileSync(path[, options])
- 流式文件读取和写入
- 流式读取和写入适用于一些比较大的文件
fs.createWriteStream(path[, options])
fs.createReadStream(path[, options])
//同步
var fs=require("fs");
var fd=fs.openSync("hello.txt","w")
fs.writeSync(fd,"Hello Word")
fs.closeSync(fd)
//异步
fs.open("hello1.txt","w",function (err,fd) {
if (!err){
fs.write(fd,"你好世界",function (err) {
if (!err){
console.log("写入成功")
}
fs.close(fd,function (err) {
if (!err){
console.log("文件关闭")
}
})
})
}
})
//简单同步写入
fs.writeFileSync("hello2.txt","你好世界",{flag:"w"})
//简单异步写入
fs.writeFile("hello3.txt","Hello Word",function (err) {
console.log("写入成功!!!")
})
var wd=fs.createWriteStream("hello3.txt",function () {
})
wd.write("天天向上")
wd.write("你好世界")
wd.end()
fs.readFile("hello3.txt",function (err,data) {
if (!err){
console.log(data.toString());
}
})
//数据流
var rs=fs.createReadStream("hello3.txt")
var ws=fs.createWriteStream("hello4.txt")
rs.on("data",function (data) {
})
rs.pipe(ws)