简单的fs文件系统模块案例

 一、创建index.html、circular.html、square.html、fs.js这四个文件

 二、复制粘贴index.html、circular.html、square.html、fs.js这四个文件的源码

index.html源码

<!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>
<h3>我是主页面,编写网址跳转页面</h3>
</body>
</html>

circular.html源码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
	<style type="text/css">
		div{
			width: 200px;
			height: 200px;
			background: green;
			border-radius: 50%;
		}
	</style>
	<link rel="stylesheet" type="text/css" href="bb" />
</head>
<body>
	<img src="pic" alt="">
	<div></div>
</body>
</html>

square.html源码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
	<style type="text/css">
		div{
			width: 200px;
			height: 200px;
			background: red;
		}
	</style>
</head>
<body>
	<div></div>
</body>
</html>

fs.js源码

var http = require("http"); //通信相关
var fs = require("fs"); //文件相关

// 使用createServer 方法创建服务器
const server = http.createServer(function (req, res) {
  // 设置响应头
  res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
  // console.log(req.url);
  if (req.url == "/") {
    fs.readFile("./index.html", (err, data) => {
      if (err) {
        return console.error(err);
      }
      res.end(data);
    });
  } else if (req.url == "/yuanxing") {
    fs.readFile("./circular.html", (err, data) => {
      if (err) {
        return console.error(err);
      }
      res.end(data);
    });
  } else if (req.url == "/fangxing") {
    fs.readFile("./square.html", (err, data) => {
      res.end(data);
    });
  } else {
    res.end("没有这个页面");
  }
});

// 运行服务器, 监听端口号3000
server.listen(3000, "127.0.0.1", () => {
  console.log("服务端,已启动,端口号: http://localhost:3000/");
  console.log("终端启动命令: node fs.js");
});

三、点开终端输入 node fs.js 命令

 运行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值