前端歌谣-第伍拾肆课-node之http模块之路由模块

前言

我是歌谣 微信公众号关注前端小歌谣一起学习前端知识 今天继续给大家讲解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

运行结果

在这里插入图片描述

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值