首页 > 技术文章 > node.js知识点

heleiya 2020-09-17 18:10 原文

一、文件的写入

非阻塞

步骤:

1、先引入fs模块===>  const fs = require("fs");

2、读取文件=====>  fs.writeFile("文件的路径","文件的内容",回调函数)

具体事例:    fs.writeFile()

 

 

 二、文件的追溯

代码事例:  fs.appendFile()

 

 

 三、文件的读取

代码事例:  fs.readFile()

 

 

 四、文件的拷贝

  代码事例  :fs.copyFile()

 

 

 五、获取文件的具体信息

代码事例:fs.stat()

 

 

六、 常用的方法:

1、isFile()判断是否是文件

2、isDirectoty() 判断是否为目录

七、删除文件 

fs.unlink()

 

 

 

 

操作目录:

1、创建目录

 

 

 2、读取目录

fs.readdir();

 

 

 

实例:读取一个目录里面的所有文件

 

 

 删除目录:

 

 

 

http模块

http模块主要是快速的搭建一个快速的web服务器

 

 get 请求

 

 

post

const http = require('http');
const url = require('url'); // node 内置模块

let str = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="" method="POST">
<div>
用户名:<input type="text" name="username" id="username">
</div>
<div>
密码:<input type="password" name="userpwd" id="userpwd">
</div>
<button>提交</button>
</form>
</body>
</html>
`;

http.createServer((req,res)=>{
let body = ''; // 用于接收 post 请求传递过来的数据
// data 事件会在客户端有数据传递过来的时候触发
req.on('data',function(chunk){
body += chunk;
});
// end 事件会在 post 请求的数据接收完毕之后触发
req.on('end',function(){
console.log(body);
res.writeHead(200,{'Content-Type' : 'text/html;charset=utf-8'});
if(body){
res.end('服务器已经接收到客户端post请求内容:' + body);
} else {
res.end(str);
}
})
}).listen(3000);
console.log('服务器已经启动...');

爬虫:

 

推荐阅读