在之前我们说了http模块的简单搭建服务器
现在我们来制作简单的响应
如果之前没看过的话可以去看看
浅谈关于nodejs的HTTP模块
模块:HTTP模块,FS文件模块(文件系统模块)
工具:你的编辑器
利用fs文件模块读取目录下的网页文件
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试</title>
<style>
a{
font-size: 20px;
transition: 1s;
}
a:hover{
font-size: 40px;
}
p{
color: aqua;
transition: 1s;
font-size: 20px;
}
p:hover{
color: rgb(12, 63, 63);
font-size: 30px;
}
</style>
</head>
<body>
<p>hello word</p>
<p>hello node-http</p>
<a href="http://baidu.com">百度</a>
</body>
</html>
app.js
const http = require('http');
const fs=require('fs');
// 导入模块
const app = http.createServer();
// 创建服务器
app.listen(3000, () => {
// 监听端口
console.log('127.0.0.1:3000');
// 回调返回数据
});
app.on('request', function (req, res) {
console.log(req.url);
var data=fs.readFileSync('./index.html')
// 自动把buffer转为字符串
res.write(data)
//获取请求的资源,请求的方式
res.end();
})
// http://localhost:3000
// http://127.0.0.1:3000
开启服务器当访问时


可以看到正常但是我们如果把css外置呢?


重启服务器后我们发现我们的css样式没了这是为什么呢?


原因如上图
当在页面上打开index.html之后它无法找到自己的样式所以就会失效