一、文件系统

文件读取与写入

  1. // 1.引入文件系统模块
  2. const fs = require('fs');
  3. // 2.通过对象调用方法
  4. //同步读取文件(文件名,文件编码)
  5. var readme = fs.readFileSync("readme.txt", "utf8");
  6. console.log(readme);
  7. //同步写入文件(文件名,写入内容)
  8. fs.writeFileSync("writeme.txt", readme);
  9. //异步读取文件(文件名,文件编码,回调函数(错误信息,读取内容))
  10. fs.readFile("readme.txt", "utf8", function (err, data) {
  11. if (err) throw err;
  12. console.log(data);
  13. })
  14. //异步写入文件
  15. fs.readFile("write.txt","utf8",function(err,data){
  16. //异步写入文件(文件名,写入内容)
  17. fs.writeFile("write.txt",data);
  18. })

创建和删除文件

  1. //1.引入文件系统模块
  2. var fs = require('fs');
  3. //2.使用模块对象调用方法
  4. //异步删除文件
  5. fs.unlink("writeme.txt", function (err) {
  6. if (err) throw err;
  7. console.log("文件删除成功");
  8. });
  9. //3.创建/删除文件夹
  10. //同步创建文件夹
  11. fs.mkdirSync("stuff");
  12. //同步删除文件夹
  13. fs.rmdirSync("stuff");
  14. //异步创建文件夹
  15. fs.mkdir("stuff", function (err) {
  16. if (err) throw err;
  17. console.log("文件夹创建成功!")
  18. })
  19. //异步删除文件夹
  20. fs.rmdir("stuff", function (err) {
  21. if (err) throw err;
  22. console.log("文件夹删除成功!")
  23. })

二、服务器与客户端

  1. //通过http模块,创建本地服务器
  2. //引入http模块
  3. var http = require('http');
  4. //创建本地服务器
  5. var server = http.createServer(function (req, res) {
  6. console.log("服务器:" + req.url);
  7. //数据头
  8. res.writeHead(200, { "Content-type": "text/plain" });
  9. res.end("Server is working ...");
  10. });
  11. //服务对象监听服务器地址以及端口号
  12. server.listen(8888, "127.0.0.1");
  13. console.log("服务器正在运行。。。");

三、数据流与管道

读写数据流

  1. //数据流
  2. var fs = require('fs');
  3. //读数据流
  4. var myReadStream = fs.createReadStream(__dirname + '/readme.txt', "utf8");
  5. //写入文件流
  6. var myWriteStream = fs.createWriteStream(__dirname + '/writeme2.txt')
  7. //管道可以实现数据转移
  8. myReadStream.pipe(myWriteStream);
  9. // console.log(myReadStream)
  10. // myReadStream.on('data', function (chunk) {
  11. // console.log('=========================一部分数据======================');
  12. // //写入数据
  13. // myWriteStream.write(chunk);
  14. // });
  1. //在浏览器中访问数据流读取的数据(通过管道pipe)
  2. var http = require('http');
  3. var fs = require('fs');
  4. var server = http.createServer(function (req, res) {
  5. res.writeHead(200, { "Content-type": "text/plain" });
  6. var myReadStream = fs.createReadStream(__dirname + '/readme.txt', "utf8");
  7. myReadStream.pipe(res);
  8. });
  9. server.listen(3000, "127.0.0.1");
  10. console.log("server is runing...");

四、Node读取HTML和JSON数据

读取html

  1. var http = require('http');
  2. var fs = require('fs');
  3. //搭建服务器
  4. //req->request:请求,res->respond:回应(响应)
  5. var server = http.createServer(function (req, res) {
  6. //文件头需要改为html类型
  7. res.writeHead(200, { "Content-type": "text/html" });
  8. var myReadStream = fs.createReadStream(__dirname + '/index.html', "utf8");
  9. //管道可以实现数据转移
  10. myReadStream.pipe(res);
  11. });
  12. //监听IP+端口
  13. server.listen(3000, "127.0.0.1");
  14. console.log("server is runing...");

读取json

  1. var http = require('http');
  2. var fs = require('fs');
  3. //搭建服务器
  4. //req->request:请求,res->respond:回应(响应)
  5. var server = http.createServer(function (req, res) {
  6. //文件头可以改为json类型
  7. res.writeHead(200, { "Content-type": "application/json" });
  8. var myReadStream = fs.createReadStream(__dirname + '/person.json', "utf8");
  9. //管道可以实现数据转移
  10. myReadStream.pipe(res);
  11. });
  12. //监听IP+端口
  13. server.listen(3000, "127.0.0.1");
  14. console.log("server is runing...");

五、路由

  1. //搭建服务器
  2. //req->request:请求,res->respond:回应(响应)
  3. var server = http.createServer(function (req, res) {
  4. //判断用户将要访问的地址(路由)
  5. if (req.url == '/home' || req.url == '/') {
  6. res.writeHead(200, { "Content-type": "text/html" });
  7. //将文件读取流中读取到的文件通过管道(pipe)发送到页面中
  8. fs.createReadStream(__dirname + '/index.html', "utf8").pipe(res);
  9. } else if (req.url == "/contact") {
  10. res.writeHead(200, { "Content-type": "text/html" });
  11. fs.createReadStream(__dirname + '/contact.html', "utf8").pipe(res);
  12. } else if (req.url == "/api/docs") {
  13. var data = [{ name: "herry", age: 43 }, { name: "bucky", age: 12 }];
  14. res.writeHead(200, { "Content-type": "application/json" });
  15. //将data值转化为json字符串
  16. res.end(JSON.stringify(data));
  17. }
  18. });
  19. server.listen(3000, "127.0.0.1");
  20. console.log("server is runing...");

六、Express模板

send是express的方法,end是原生的方法,而且使用了end也可以返回数据

  1. //引入express框架
  2. var express = require('express');
  3. //实例化express对象
  4. var app = express()
  5. //通过对象调用方法
  6. //根据用户请求的地址返回相应的数据信息,get(路径,函数)
  7. app.get('/', function (req, res) {
  8. console.log(req.url);
  9. //发送到浏览器的信息
  10. res.send("this is home page!")
  11. })
  12. app.get("/contact", function (req, res) {
  13. console.log(req.url);
  14. res.send("thid id contact page!")
  15. })
  16. //路由参数
  17. app.get("/profile/:id", function (req, res) {
  18. //params可以获取路由的参数
  19. res.send("路径参数为:" + req.params.id)
  20. })
  21. //监听服务器端口号
  22. app.listen(8888);
  • EJS模板引擎

  1. //引入express框架
  2. var express = require('express');
  3. //实例化express对象
  4. var app = express()
  5. //配置视图引擎
  6. app.set('view engine', 'ejs');
  7. //通过对象调用方法
  8. //根据用户请求的地址返回相应的数据信息,get(路径,函数)
  9. // app.get('/', function (req, res) {
  10. // console.log(req.url);
  11. // //发送到浏览器的信息
  12. // res.sendFile(__dirname + "/index.html");
  13. // })
  14. // app.get("/contact", function (req, res) {
  15. // console.log(req.url);
  16. // res.sendFile(__dirname + '/contact.html');
  17. // })
  18. //路由参数
  19. app.get("/profile/:id", function (req, res) {
  20. //使用render(文件名,传入参数),访问.ejs文件,只需给出文件名,无需后缀名(提前创建了一个views>profile.ejs)
  21. res.render('profile', { name: 'EJS' });
  22. })
  23. app.listen(8888);
  1. //路由参数
  2. app.get("/profile/:id", function (req, res) {
  3. var data = [{ age: 29, name: "herry" }, { age: 33, name: "bob" }];
  4. //render(文件名,传入参数),访问.ejs文件,只需给出文件名,无需后缀名
  5. res.render('profile', { websiteName: req.params.id, data: data });
  6. })
  7. // views>profile.ejs中部分代码
  8. <h1>Welcome to <%= websiteName %></h1>
  9. <!-- <p><strong>Age:</strong><%= data.age %></p>
  10. <p><strong>name:</strong><%= data.name %></p> -->
  11. <ul>
  12. <% data.forEach(function(item){%>
  13. <li><%= item.age %></li>
  14. <li><%= item.name %></li>
  15. <% }) %>
  16. </ul>
  17. 监听服务器端口号