一、简介:
1.什么是后端开发:
-
前端与后端:
前端指的是在客户端上运行的代码;
后端是运行在服务器端的程序。 -
后端开发组成
语言是:PHP/JAVA/C#/JavaScrpit;
Web服务器:Apach/Tomcat;
数据库:MySQL/Oricle/MongoDB -
后端开发平台
LAMP JavaEE .NET Node.js
二、 Node.js组成
三、核心模块
同步&异步
- 所有的方法都有同步和异步的形式,node里关于文档的操作方式基本都是两份,既同步与异步
- 使用前倒入核心模块fs const fs = require(“fs”);
//重命名 异步
fs.rename("ok.txt","yes.txt",function(err){
if(err)thorw err;
console.log("重命名成功");
})
// 重命名 异步
fs.renameSync("ok.txt","yes.txt");
fs
- 文件操作
// 1. 读文件 fs.readFlie();
fs.readFlie("./yes.txt",function(err,data){
if(err) throw err;
console.log(data);
//data 是一个buffer
})
fs.readFlie("./yes.txt","utf8",function(err,data){
if(err) throw err;
console.log(data);
//以指定编码输出
})
// 2. 写文件 fs.writeFlie()
fs.writeFlie("./yes.txt","学习使我快乐!",function(err){
if(err) throw err;
})
// 默认就是utf8的编码格式
// 效果:删掉文档原内容,写入新内容,如果指定文档不存在,则新建
// 3. 追加 fs.appendFile()
for(let i=0; i<10; i++) {
fs.appendFlie("./yes.txt","item"+i+"\n",function(err){
if(err) throw err;
})
}
// 4. 拷贝 fs.copyFile()
fs.copyFlie("./yes.txt","./ok.txt",function(err){
if(err) throw err;
})
- 流操作
//一般读取大文件
// 读取流
const fs = require("fs");
var rs = fs.createReadStream("./ok.txt",{encoding:"utf8"});
rs.on("open",function(){
console.log("可读流开启")
})
rs.on("data",function(){
console.log(chunk);
})
rs.on("end",function(){
console.log("可读流读取结束")
})
rs.on("close",function(){
console.log("可读流关闭")
})
// 写入流
const fs = require("fs");
var ws = fs.createWriteStream("./ok.txt");
ws.write("今天也得学习");
ws.end();
ws.on("open",function() {
console.log("可写流开启")
})
ws.on("close",function() {
console.log("可写流关闭");
})