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中路由器控制的实现代码的详细内容。
