const fs=require('fs');
const http=require('http');
const app=http.createServer((req,res)=>{
console.log("server connected");
res.writeHead(200,http.STATUS_CODES[200],{
"Content-type":"text/html;charset=utf-8"
})
let content=""
switch (req.url) {
case "/":
staticSend("/static/index.html")
break;
case "/list":
staticSend("/static/list.html")
break;
case "/index.css":
staticSend("/static/index.css",{"Content-type":"text/css;charset=utf-8"})
break;
default:
staticSend("/static/404.html",{"Content-type":"text/html;charset=utf-8"},404)
break
}
function staticSend(filename,headers={"Content-type":"text/html;charset=utf-8"},statusCode=200){
res.writeHead(statusCode,http.STATUS_CODES[statusCode],headers)
content=fs.readFileSync(__dirname+filename)
res.end(content)//end的同时把内容发送到客户端
}
})
app.listen(80,()=>{
console.log("app start at port 80");
})
nodejs之http配合fs模块向客户端发送数据
最新推荐文章于 2022-07-28 18:36:45 发布