Node學習隨筆—http相關__http模塊&路由

一:http模塊

http模塊是node的常用模塊,可以用瀏覽器訪問寫的代碼

1.引進http模塊(核心模塊不需要安裝)

 let http = require("http")

2.創(chuàng)建服務器(參數接受函數)

 let server = http.createServer((req, res)=>{
    // 返回結果(狀態(tài)碼,返回類型, 返回的編碼)
    res.writeHead(200,{"Content-type": "text/html"; charset=UTF-8})
    // 瀏覽器內容
    res.write("111")
    res.write("<h1>哈哈哈哈哈</h1>")
    res.end("hello")
})

3.監(jiān)聽(知道訪問,參數端口號,地址locallhost)

server.listen(80, "127.0.0.1")

二:路由

1.node只能通過控制url來控制跳轉,在服務器端先配置好路由

2.配置路由

引入讀寫操作文件

let fs = require(fs)

 let server = http.createServer((req, res)=>{
    if (req.url==='/test1.html') { // 瀏覽器地址欄的路由url
        console.log(111)
        // 讀入文件
        fs.readFile("./test1.html", (err, data)=>{
            if (!err){
                console.log(data.toString)
                res.writeHead(200,{"Content-type": "text/html"; charset=UTF-8})
                res.end(data)
            }
        })
    } else if (req.url==='/test2.html') {
        console.log(222)
        // 讀入文件
        fs.readFile("./test2.html", (err, data)=>{
            if (!err){
                console.log(data.toString)
                res.writeHead(200,{"Content-type": "text/html"; charset=UTF-8})
                res.end(data)
            }

        })
    } else if (req.url==='/') {


    }else {
        // 返回結果(狀態(tài)碼,返回類型, 返回的編碼)
        res.writeHead(404,{"Content-type": "text/html"; charset=UTF-8})
        // 瀏覽器內容
        res.end("訪問頁面不存在")
    }
})

3.node中加載其他類型資源

---除了html和js文件除外的類型文件的訪問

比如css文件,img圖片, 音頻等,要用fs讀到服務中如下

 let server = http.createServer((req, res)=>{
    if (req.url==='/test1.html') { // 瀏覽器地址欄的路由url, 路由1
        console.log(111)
        // 讀入文件
        fs.readFile("./test1.html", (err, data)=>{
            if (!err){
                console.log(data.toString)
                res.writeHead(200,{"Content-type": "text/html"; charset=UTF-8})
                res.end(data)
            }
        })
    } else if (req.url==='/test2.html') { // 瀏覽器地址欄的路由url, 路由2
        console.log(222)
        // 讀入文件
        fs.readFile("./test2.html", (err, data)=>{
            if (!err){
                console.log(data.toString)
                res.writeHead(200,{"Content-type": "text/html"; charset=UTF-8})
                res.end(data)
            }

        })
    } else if (req.url==='/css/index/.css') { // css
        // 讀css文件
        fs.readFile("./css/index/.css", (err, data)=>{
            if (!err){
                console.log(data.toString)
                res.writeHead(200,{"Content-type": "text/css"}) // text/css
                res.end(data)
            }
        })
    }else if (req.url==='/img/lixiaoyu.png') { // 圖片
        // 讀圖片文件
        fs.readFile("./img/lixiaoyu.png", (err, data)=>{
            if (!err){
                console.log(data.toString)
                res.writeHead(200,{"Content-type": "image/jpg"}) // image/jpg
                res.end(data)
            }
        })
    } else {
        // 返回結果(狀態(tài)碼,返回類型, 返回的編碼)
        res.writeHead(404,{"Content-type": "text/html"; charset=UTF-8})
        // 瀏覽器內容
        res.end("訪問頁面不存在")
    }
})

小節(jié)圖如下:
image
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,511評論 19 139
  • Node.js第一天 1. 初識Node.js 1.1 Node.js是什么 Node.js? is a Java...
    再見天才閱讀 4,893評論 1 24
  • 作者: Manuel Kiessling 翻譯: goddyzhao & GrayZhang & MondayC...
    紫月凌楓閱讀 2,473評論 5 26
  • 晨間的少年啊 你是什么時候長大的呢 褪去青澀 換上大人模樣 晨間的少年啊 你是什么時候長大的呢 稚嫩的肩膀 撐起了...
    慕青檸閱讀 291評論 2 1
  • 其實打小開始 我就不太拒絕人 所以朋友說去干嘛干嘛吧 我說好啊去吧 即使有時候我不怎么想去 但是每次我都會想著去不...
    馬田心Martinc手作閱讀 360評論 2 1

友情鏈接更多精彩內容