本篇文章给大家带来的内容是关于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> <img src="/image/1.jpg" alt="">
show.html:
<h1>你好,全世界</h1>
style.css:
h1 {
font-size: 18px;
}
img{
width: 50%;
}相关推荐:
PHP网络编程技术详解由浅入深,全面、系统地介绍了PHP开发技术,并提供了大量实例,供读者实战演练。另外,笔者专门为本书录制了相应的配套教学视频,以帮助读者更好地学习本书内容。这些视频和书中的实例源代码一起收录于配书光盘中。本书共分4篇。第1篇是PHP准备篇,介绍了PHP的优势、开发环境及安装;第2篇是PHP基础篇,介绍了PHP中的常量与变量、运算符与表达式、流程控制以及函数;第3篇是进阶篇,介绍
386
以上就是Node.js中路由器控制的实现代码的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号