前言
我是歌谣 微信公众号关注前端小歌谣一起学习前端知识 今天继续给大家讲解node中路由模块的讲解
启动服务器
const http=require("http")
http.createServer((req,res)=>{
const myurl=new URL(req.url,"http://127.0.0.1")
console.log(myurl.pathname)
}).listen(3000,()=>{
console.log("server start")
})
运行结果
路由案例
const http = require("http")
const fs = require("fs")
http.createServer((req, res) => {
const myurl = new URL(req.url, "http://127.0.0.1")
// console.log(myurl.pathname)
switch (myurl.pathname) {
case "/login":
res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
res.write(fs.readFileSync("./static/login.html", "utf8"))
break
case "/home":
res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
res.write(fs.readFileSync("./static/home.html", "utf8"))
break
default:
res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
res.write(fs.readFileSync("./static/404.html", "utf8"))
}
res.end()
}).listen(3000, () => {
console.log("server start")
})
运行结果
抽离
route.js
const fs = require("fs")
function route(res,pathname){
switch (pathname) {
case "/login":
res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
res.write(fs.readFileSync("./static/login.html", "utf8"))
break
case "/home":
res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
res.write(fs.readFileSync("./static/home.html", "utf8"))
break
default:
res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
res.write(fs.readFileSync("./static/404.html", "utf8"))
}
}
module.exports = route
index.js
const http = require("http")
const route=require("./route.js")
http.createServer((req, res) => {
const myurl = new URL(req.url, "http://127.0.0.1")
// console.log(myurl.pathname)
route(res,myurl.pathname)
res.end()
}).listen(3000, () => {
console.log("server start")
})
运行结果
优化之后
index.js
const http = require("http")
const route=require("./route.js")
http.createServer((req, res) => {
const myurl = new URL(req.url, "http://127.0.0.1")
// console.log(myurl.pathname)
route[myurl.pathname](res)
res.end()
}).listen(3000, () => {
console.log("server start")
})
route.js
const fs = require("fs")
// function route(res,pathname){
// switch (pathname) {
// case "/login":
// res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
// res.write(fs.readFileSync("./static/login.html", "utf8"))
// break
// case "/home":
// res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
// res.write(fs.readFileSync("./static/home.html", "utf8"))
// break
// default:
// res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
// res.write(fs.readFileSync("./static/404.html", "utf8"))
// }
// }
const route={
"/login":(res)=>{
res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
res.write(fs.readFileSync("./static/login.html", "utf8"))
},
"/home":(res)=>{
res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
res.write(fs.readFileSync("./static/home.html", "utf8"))
},
"/404":(res)=>{
res.writeHead(200, { "content-Type": "text/html;charset=utf-8" })
res.write(fs.readFileSync("./static/404.html", "utf8"))
}
}
module.exports = route