您好,欢迎来到多米娱乐网。
搜索
您的当前位置:首页Node.js中路由器控制的实现代码

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

来源:多米娱乐网


本篇文章给大家带来的内容是关于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%;
}

Copyright © 2019- dmir.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务