獲取模塊 const fs = require("fs")
css
demohtml
const read = fs.createReadStream("./sjl.txt"); //讀取流
let str = "";
read.setEncoding("utf8"); //設置字符集
// read.resume(); 改變流的狀態爲釋放狀態,讓它開始動起來
//read中的內置事件
//設置流狀態爲釋放狀態,回調中的第一個形參爲讀取的數據
read.on("data", (data) =>{
str = data;
});
//流釋放完畢後,觸發end事件中的回調
read.on("end", ()=>{
console.log(str)
![](https://user-gold-cdn.xitu.io/2018/12/11/1679bfa086c05c90?w=280&h=51&f=png&s=9409)
console.log("讀取結束");
})
複製代碼
效果: 讀取sjl.txt中的內容jquery
2.fs.appenfFile和fs.appendFileSyncajax
前者經過異步的方式將文本內容或數據添加到文件裏,若是文件不存在會自動建立。跨域
fs.appendFile("./sjl2.txt", "sjl", "utf8", (err) => {
console.log(err);
});
複製代碼
後者經過同步的方式將文本內容或數據添加到文件裏,文件不存在則自動建立。bash
const write = fs.createWriteStream("./sjl2.txt");
write.write("11111");
複製代碼
//管道 pipe
const read = fs.createReadStream("./sjl.txt");
const write = fs.createWriteStream("./sjl2.txt");
read.pipe(write); //將sjl.txt的內容給予sjl2.txt,至關於複製文件
複製代碼
const Readable = require("stream").Readable; //獲得可讀流的構造函數/類
const read = new Readable(); //建立一個可讀流對象
read.setEncoding("utf8");
read.push("123");
read.push("456");
read.push("789"); //放數據
read.push(null); //數據放完了
read.on("data", data => {
console.log(data);
});
複製代碼
引入模塊const http = require("http");
app
const http = require("http");
//建立一個服務
const server = http.createServer((req, res)=>{
/*
1. req: 請求體對象
2. res: 響應體對象,後臺給客戶端的響應
*/
//設置響應頭(狀態碼,響應內容類型)
res.writeHead(200, {
"Content-Type" : "text/html;charset=utf-8"
});
//text/plain :純文本 text/html:標籤
res.write("返回的數據"); //向客戶端返回數據,可調用無數次
res.end(""); //響應結束
//write和end中參數必須是string或者Buffer對象
});
srever.listen(3000, ()=>{
console.log("服務監聽在localhost 3000端口")
}); //監聽3000端口
複製代碼
const http = require("http");
//建立一個服務
const server = http.createServer((req, res) => {
// req 請求體對象 res:響應體對象
req.url; //路由
req.method; //方法 "GET","POST"
req.headers; //請求頭內容
});
//監聽端口號
server.listen(3000);
console.log("開始監聽3000端口");
複製代碼
const http = require("http");
//建立一個服務
const server = http.createServer((req, res) => {
if (req.url !== "/favicon.ico") { //拒絕圖標請求
if (req.method === "GET") { //設置方法
res.writeHead(200, {
"Content-Type": "text/html;charset=utf-8"
});
switch (req.url) {
case "/sjl":
res.write("<div>sjl</div>");
break;
case "/":
res.write("<div>根目錄</div>");
break;
default:
res.write("<div>沒有符合要求的</div>");
break;
}
}
}
res.end();
});
//監聽端口號
server.listen(3000);
複製代碼
訪問localhost:3000 的效果圖: 異步
新建兩個html文件 a.html和b.html函數
const http = require("http");
const fs = require("fs");
//建立一個服務
const server = http.createServer((req, res) => {
if (req.method === "GET") {
res.writeHead(200, {
"Content-Type": "text/html;charset=utf-8"
});
switch (req.url) {
case "/sjl":
fs.readFile("./01.html", "utf8", (err, data) => {
res.write(data);
res.end();
});
break;
default:
fs.readFile("./default.html", "utf8", (err, data) => {
res.write(data);
res.end();
});
break;
}
}
});
//監聽端口號
server.listen(3000);
console.log("開始監聽3000端口");
複製代碼
const http = require("http");
const fs = require("fs");
const server = http.createServer((req, res) => {
if (req.method === "GET") {
res.writeHead(200, {
"Content-Type": "text/html;charset=utf-8"
});
switch (req.url) {
case "/sjl":
res.write(fs.readFileSync("./01.html", "utf8"));
break;
default:
res.write(fs.readFileSync("./default.html", "utf8"));
break;
}
}
res.end();
});
//監聽端口號
server.listen(3000);
console.log("開始監聽3000端口");
複製代碼
效果圖:ui
建立一個html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax案例</title>
<style>
*{margin:0;padding:0;}
li{list-style:none;}
a{text-decoration: none;}
#wrap{
width:200px;
height:200px;
background-color:#f66;
}
</style>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div id="wrap">ajax案例</div>
<script>
$("#wrap").click(() => {
$.ajax({
url : "http://127.0.0.1:3000",
method : "get",
success : function(msg){
console.log(msg);
}
});
});
</script>
</body>
</html>
複製代碼
建立http服務,獲取數據
const http = require("http");
const server = http.createServer((req, res) => {
//設置容許跨域,CROS跨域
res.setHeader("access-control-allow-origin", "*");
res.write("傳出的數據");
console.log(15326235135);
res.end();
});
server.listen(3000);
console.log("開始監聽3000端口");
複製代碼
點擊頁面時效果: