3.nodejs(三) 常用API (4)

res:

$ node fs62 null fs63.js var fs = require("fs"); //判断文件和目录 //文件状态 文件、目录是否存在 是文件 还是目录 fs.stat("abc", (err,stats) => { //stats.isDirectory()是否是目录/stats.isFile()是否是文件 console.log(err,stats.isFile(),stats.isDirectory()); });

res:

$ node fs63 null false true fs71.js var fs = require("fs"); //可以监听文件和目录 //监听有范围限制 只有一层 fs.watch("a.txt", (eventType, filename) => { console.log(`事件类型是: ${eventType}-----${filename}`); });

res:

$ node fs71 事件类型是: change-----a.txt 事件类型是: change-----a.txt fs72.js var fs = require("fs"); //可以监听文件 目录 有延迟 目录的文件的修改监听不到 最好用来监听文件 fs.watchFile("abc", (curr, prev) => { /*console.log(curr); console.log(prev);*/ console.log(curr.mtime); }); /*Stats { dev: 4145068239, mode: 16822, nlink: 1, uid: 0, gid: 0, rdev: 0, blksize: undefined, ino: 844424930155958, size: 0, blocks: undefined, atimeMs: 1531312004235.5886, mtimeMs: 1531312004235.5886, ctimeMs: 1531312004235.5886, birthtimeMs: 1531311081220.7952, atime: 2018-07-11T12:26:44.236Z, mtime: 2018-07-11T12:26:44.236Z, ctime: 2018-07-11T12:26:44.236Z, birthtime: 2018-07-11T12:11:21.221Z } */

res:

$ node fs72 2018-07-11T12:24:44.050Z 6.流stream

流stream

输入流、输出流

var rs = fs.createReadStream("src");
var ws = fs.createWriteStream("dist");

管道pipe

rs.pipe(ws);

拆解:

//一边读一边写

rs.on("data",function(data){ ws.wrtie(data); }); rs.on("end",function(data){ ws.end(fn); });

rs.on("error",function(){...})
ws.on("error",function(){...})
---

gzip: 1、const zlib = require('zlib'); 2、必须设置一个头:

res.writeHead(200, { 'content-encoding': 'gzip' });

3、通过管道

rs.pipe(zlib.createGzip()).pipe(res);

stream1.js var fs = require("fs"); //输入流 输出 var rs = fs.createReadStream("a.txt"); var ws = fs.createWriteStream("b.txt"); //管道 pipe rs.pipe(ws);将a.txt中的内容一步一步写入b.txt

res:

$ node stream1 stream2.js var fs = require("fs"); //输入流 var rs = fs.createReadStream("a.txt"); rs.on("data",function(data){ console.log(data); }); rs.on("end",function(){ console.log("读完了"); }); rs.on("error",function(){ console.log("读取文件失败"); });

res:

$ node stream2 <Buffer 61 61 61> 读完了 stream3.js var fs = require("fs"); var ws = fs.createWriteStream("b.txt"); ws.write("abc",function(){ console.log("write完了"); }); ws.end("end",function(){ console.log("end写完了"); });

res:

$ node stream3 write完了 end写完了 stream4.js var fs = require("fs"); var rs = fs.createReadStream("a.txt"); var ws = fs.createWriteStream("b.txt"); rs.on("data",function(data){ ws.write(data); }); rs.on("end",function(){ console.log("读完了"); ws.end(function(){ console.log("写完了"); }); });

res:

$ node stream4 读完了 写完了 server.js

前面的服务器可以写成流
大文件可以压缩

var http = require("http"); var fs = require("fs"); var zlib = require("zlib"); var server = http.createServer(function(req,res){ var url = req.url; if(url == "/favicon.ico") return; /*fs.readFile("www"+url,function(err,data){ if(err){ res.end("404"); } else { res.end(data); } });*/ res.writeHead(200, { 'content-encoding': 'gzip' }); var rs = fs.createReadStream("www"+url); rs.pipe(zlib.createGzip()).pipe(res); }); server.listen(9000); 完整服务器代码:

server.js

var http = require("http"); var fs = require("fs"); var zlib = require("zlib"); var modUrl = require("url"); var qs = require("querystring"); var users = {aaa:123}; var server = http.createServer(function(req,res){ var url = req.url; var str = ""; req.on("data",function(data){ str += data; }) req.on("end",function(){ var urlObj = modUrl.parse(url,true); url = urlObj.pathname; var GET = urlObj.query; var POST = qs.parse(str); var params = req.method == "POST" ? POST : GET; var username = params.user; var password = params.pass; if(url == "/login"){ if(users[username]){ if(users[username] == password){ res.end(JSON.stringify({"error":1,"msg":"登陆成功"})); }else{ res.end(JSON.stringify({"error":0,"msg":"用户名或者密码错误"})); } }else{ res.end(JSON.stringify({"error":0,"msg":"用户名或者密码错误"})); } }else if(url == "/reg"){ if(users[username]){ res.end(JSON.stringify({"error":0,"msg":"用户名已存在"})); }else{ users[username] = password; res.end(JSON.stringify({"error":1,"msg":"注册成功"})); } }else if(url == "/favicon.ico") { return; }else{ res.writeHead(200, { 'content-encoding': 'gzip' }); var rs = fs.createReadStream("www"+url); rs.pipe(zlib.createGzip()).pipe(res); console.log(res); } }) }); server.listen("9000",function(){ console.log("服务器已打开!"); })

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpyyfz.html