/* 创建模块 */
const fs = require("fs");
const path = require("path");
const http = require("http");
/* 创建服务器 */
http.createServer((req, res) => {
/* 判断是不是为空 */
if (req.url == "/") {
console.log(req.url)
fs.readFile(path.join(__dirname, "group.html"), "utf-8", (err, data) => {
if (err) {
console.log(err);
res.end(JSON.stringify(err))
return
}
res.setHeader("Content-Type", "text/html;charset=utf-8;")
//前后端交互时,只能请求和响应字符串。
res.end(data);
})
/* 判断是不是1开头的 */
} else {
//请求的是静态资源 html引入的图片,css,js等。
fs.readFile(path.join(__dirname, req.url), (err, data) => {
if (err) {
console.log(err);
res.end(JSON.stringify(err))
return
}
//前后端交互时,只能请求和响应字符串。
res.end(data);
})
}
}).listen(3000);
console.log("服务器启动成功")