Node.js(服务器端的程序语言)
Node:一门后端语言(服务器端的程序语言),能够连接数据库存取数据,能够接受和处理网络请求(服务器的响应,发送请求去获取数据),单线程事件驱动,异步执行,不等待,提高IO(input和ouput)的处理速度和效率。
服务器:本质上是一台PC主机(linux系统,windows系统),部署了后端语言的执行环境,并且能够长时间提供网络服务。
### 事件驱动
node本身提供了事件对象,帮助我们快速订阅者模式,或者观察者模式,或者事件模式。
//事件的订阅
event.on(‘林俊杰演唱会’,()=>{订阅门票})
//事件的触发
event.emit(‘林俊杰演唱会’)
### 读写事件
fs.readfile(‘path’,读取配置,(err,data)=>{})
fs.writeFile(‘path’,写入数据,写入配置,()=>{})
### 读写的promise封装
```javascript
let fs = require('fs')
function fsRead(path){
return new Promise(function(resolve,reject){
fs.readFile(path,{flag:'r',encoding:"utf-8"},function(err,data){
if(err){
//console.log(err)
//失败执行的内容
reject(err)
}else{
//console.log(data)
//成功执行的内容
resolve(data)
}
//console.log(456)
})
})
}
function fsWrite(path,content){
return new Promise(function(resolve,reject){
fs.writeFile(path,content,{flag:"a",encoding:"utf-8"},function(err){
if(err){
//console.log("写入内容出错")
reject(err)
}else{
r