您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Node.js中路由器控制的实现代码

2024/3/19 13:43:04发布26次查看
本篇文章给大家带来的内容是关于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%;}
相关推荐:
nodejs路由与控制器的使用
以上就是node.js中路由器控制的实现代码的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product