本篇文章给大家带来的内容是关于Node.js中路由器控制的实现代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
render.js:
//引入模块
let http = require("http");
let fs = require("fs");
//创建HTTP服务
http.createServer(function (req,res) {
if (req.url === "/favicon.ico"){
return false;
}
if (req.url === "/" || req.url === "/index.html"){
// 读取文件
fs.readFile("./index.html",function (err,data) {
//设置响应头
res.writeHead(200,{
"Content-type":"text/html;charset=utf-8"
});
//结束响应
res.end(data);
})
}else if(req.url === "/show.html"){
// 读取文件
fs.readFile("./show.html",function (err,data) {
//设置响应头
res.writeHead(200,{
"Content-type":"text/html;charset=utf-8"
});
//结束响应
res.end(data);
})
}else if(req.url === "/image/1.jpg" ){
//读取文件
fs.readFile("./image/1.jpg",function (err,data) {
//设置响应头
res.writeHead(200,{
"Content-type":"image/jpeg"
});
//结束响应
res.end(data);
})
}else if(req.url === "/style.css"){
//读取文件
fs.readFile("./style.css",function (err,data) {
//设置响应头
res.writeHead(200,{
"Content-type":"text/css"
});
// 结束响应
res.end(data);
})
} else {
res.writeHead(404,{
"Content-type":"text/html;charset=utf-8"
});
res.end("您访问的也没不存在");
}
console.log(req.url);
}).listen(3200,function () {
console.log("Http Server running on port 3200");
})index.html:
<h1>你好</h1> @@##@@
show.html:
<h1>你好,全世界</h1>
style.css:
h1 {
font-size: 18px;
}
img{
width: 50%;
}相关推荐:
TURF(开源)权限定制管理系统(以下简称“TURF系统”),是蓝水工作室推出的一套基于软件边界设计理念研发的具有可定制性的权限管理系统。TURF系统充分考虑了易用性,将配置、设定等操作进行了图形化设计,完全在web界面实现,程序员只需在所要控制的程序中简单调用一个函数,即可实现严格的程序权限管控,管控力度除可达到文件级别外,还可达到代码级别,即可精确控制到
0
以上就是Node.js中路由器控制的实现代码的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号