最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Node.js中http模塊和導出共享問題

 更新時間:2022年10月24日 11:48:18   作者:前端雜貨鋪  
這篇文章主要介紹了Node.js中http模塊和導出共享,通過?http?模塊提供的?http.createServer()?方法,就能方便的把一臺普通的電腦,變成一臺?web?服務器,從而對外提供?web?資源服務,本文給大家詳細講解,需要的朋友可以參考下

一、http 模塊

http 模塊是 Node.js 官方提供的、用來創(chuàng)建 web 服務器的模塊。

通過 http 模塊提供的 http.createServer() 方法,就能方便的把一臺普通的電腦,變成一臺 web 服務器,從而對外提供 web 資源服務。

1、創(chuàng)建 web 服務器

  • 導入 http 模塊
  • 創(chuàng)建 web 服務器實例
  • 為服務器實例綁定 request 事件,監(jiān)聽客戶端的請求
  • 啟動服務器

示例:監(jiān)聽 8080 服務

// 導入 http 模塊
const http = require('http')
// 創(chuàng)建 web 服務器實例
const server = http.createServer()
// 為服務器實例綁定 request 事件 監(jiān)聽客戶端的請求
server.on('request', function (req, res) {
    console.log('請求中...')
})
// 啟動服務
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})

2、req 請求對象

只要服務器接收到了客戶端的請求,就會調(diào)用通過 server.on() 為服務器綁定的 request 事件處理函數(shù)

示例:在事件處理函數(shù)中,訪問與客戶端相關(guān)的數(shù)據(jù)或?qū)傩?/strong>

// 導入 http 模塊
const http = require('http')
// 創(chuàng)建 web 服務器實例
const server = http.createServer()
// req 是請求對象 包含了與客戶端相關(guān)的數(shù)據(jù)和屬性
server.on('request', (req) => {
    // req.url 客戶端請求的 url 地址
    const url = req.url
    // req.method 是客戶端請求的 method 類型
    const method = req.method
    const str = `Your request url is ${url} and request method is ${method}`
    console.log(str)
})
// 啟動服務
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})

3、res 響應對象

在服務器的 request 事件處理函數(shù)中,如果想訪問與服務器相關(guān)的數(shù)據(jù)或?qū)傩裕枰褂?response

示例:請求響應

// 導入 http 模塊
const http = require('http')
// 創(chuàng)建 web 服務器實例
const server = http.createServer()
// req 是請求對象 包含了與客戶端相關(guān)的數(shù)據(jù)和屬性
server.on('request', (req, res) => {
    // req.url 客戶端請求的 url 地址
    const url = req.url
    // req.method 是客戶端請求的 method 類型
    const method = req.method
    const str = `Your request url is ${url} and request method is ${method}`
    console.log(str)
    // 調(diào)用 res.end() 方法 向客戶端響應一些內(nèi)容
    res.end(str)
})
// 啟動服務
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})

4、解決中文亂碼問題

當調(diào)用 res.end() 方法,向客戶端發(fā)送中文內(nèi)容時,會出現(xiàn)亂碼問題,需要手動設(shè)置內(nèi)容的編碼格式

示例:解決中文亂碼

// 導入 http 模塊
const http = require('http')
// 創(chuàng)建 web 服務器實例
const server = http.createServer()
// req 是請求對象 包含了與客戶端相關(guān)的數(shù)據(jù)和屬性
server.on('request', (req, res) => {
    // req.url 客戶端請求的 url 地址
    const url = req.url
    // req.method 是客戶端請求的 method 類型
    const method = req.method
    const str = `請求地址是 ${url} 請求方法是 ${method}`
    console.log(str)
    // 設(shè)置 Content-Type 響應頭 解決中文亂碼問題
    res.setHeader('Content-Type', 'text/html; charset=utf-8')
    // 調(diào)用 res.end() 方法 向客戶端響應一些內(nèi)容
    res.end(str)
})
// 啟動服務
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})

5、根據(jù)不同的 url 響應不同的 html 內(nèi)容

示例:步驟如下

  • 獲取請求的 url 地址
  • 設(shè)置默認的響應內(nèi)容為 404 Not found
  • 判斷用戶請求的是否為 / 或 /index.html 首頁
  • 判斷用戶請求的是否為 /about.html 關(guān)于頁面
  • 設(shè)置 Content-Type 響應頭,防止中文亂碼
  • 使用 res.end() 把內(nèi)容響應給客戶端
// 導入 http 模塊
const http = require('http')
// 創(chuàng)建 web 服務器實例
const server = http.createServer()
// req 是請求對象 包含了與客戶端相關(guān)的數(shù)據(jù)和屬性
server.on('request', (req, res) => {
    // req.url 客戶端請求的 url 地址
    const url = req.url
    // 設(shè)置默認的內(nèi)容為 404 Not Found
    let content = '<h1>404 Not Found!</h1>'
    // 用戶請求頁是首頁
    if(url === '/' || url === '/index.html') {
        content = '<h1>首頁</h1>'
    } else if (url === '/about.html') {
        content = '<h1>關(guān)于頁面</h1>'
    }
    // 設(shè)置 Content-Type 響應頭 防止中文亂碼
    res.setHeader('Content-Type', 'text/html; charset=utf-8')
    // 調(diào)用 res.end() 方法 向客戶端響應一些內(nèi)容
    res.end(content)
})
// 啟動服務
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})

二、Node.js 中的模塊分類

1、三大模塊分類

  • 內(nèi)置模塊:由 node.js 官方提供的,如 fs、path、http 等
  • 自定義模塊:用戶創(chuàng)建的每個 .js 文件,都是自定義模塊
  • 第三方模塊:由第三方開發(fā)出來的模塊,使用前要先下載

2、模塊作用域

防止了全局變量污染的問題

示例:

index.js 文件

const username = '張三'

function say() {
    console.log(username);
}

test.js 文件

const custom = require('./index')

console.log(custom)

3、module.exports 對象

在自定義模塊中,可以使用 module.exports 對象,將模塊內(nèi)的成員共享出去,供外界使用。

外界 require() 方法導入自定義模塊時,得到的就是 module.exports 所指向的對象

示例:

index.js 文件

const blog = '前端雜貨鋪'

// 向 module.exports 對象上掛載屬性
module.exports.username = '李四'
// 向 module.exports 對象上掛載方法
module.exports.sayHello = function () {
    console.log('Hello!')
}
module.exports.blog = blog

test.js 文件

const m = require('./index')
console.log(m)

4、共享成員時的注意點

使用 require() 方法導入模塊時,導入的結(jié)果,永遠以 module.exports 指向的對象為準

示例:

index.js 文件

module.exports.username = '李四'
module.exports.sayHello = function () {
    console.log('Hello!')
}
// 讓 module.exports 指向一個新對象
module.exports = {
    nickname: '張三',
    sayHi() {
        console.log('Hi!')
    }
}

test.js 文件

const m = require('./index')
console.log(m)

5、exports 和 module.exports

默認情況下,exports 和 module.exports 指向同一個對象。

最終共享的結(jié)果,還是以 module.exports 指向的對象為準。

示例:

index1.js 文件

exports.username = '雜貨鋪'
module.exports = {
    name: '前端雜貨鋪',
    age: 21
}

index2.js 文件

module.exports.username = 'zs'
exports = {
    gender: '男',
    age: 22
}

index3.js 文件

exports.username = '雜貨鋪'
module.exports.age = 21

index4.js 文件

exports = {
    gender: '男',
    age: 21
}
module.exports = exports

module.exports.username = 'zs'

對 index2.js 文件結(jié)果的解析如下:

對 index4.js 文件結(jié)果的解析如下:

注意:為防止混亂,盡量不要在同一個模塊中同時使用 exports 和 module.exports

到此這篇關(guān)于Node.js中http模塊和導出共享的文章就介紹到這了,更多相關(guān)node.js http模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • nodejs 生成和導出 word的實例代碼

    nodejs 生成和導出 word的實例代碼

    前段時間由于項目需求,得做excel和word的導出功能.這篇文章主要介紹了nodejs 生成和導出 word的實例代碼,需要的朋友可以參考下
    2018-07-07
  • 使用Puppeteer實現(xiàn)頁面遍歷的示例代碼

    使用Puppeteer實現(xiàn)頁面遍歷的示例代碼

    很多時候我們需要遍歷我們的頁面來檢查頁面是否存在問題,以更好的保證可用性和安全性,下面就來講講如何使用puppeteer來實現(xiàn)頁面遍歷的功能吧
    2023-06-06
  • 前端自動化開發(fā)之Node.js的環(huán)境搭建教程

    前端自動化開發(fā)之Node.js的環(huán)境搭建教程

    這篇文章主要介紹了前端自動化開發(fā)之Node.js環(huán)境搭建的相關(guān)資料,文中介紹的非常詳細,對大家學習或者使用node.js具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • Node8中AsyncHooks異步生命周期

    Node8中AsyncHooks異步生命周期

    這篇文章主要介紹了Node8中AsyncHooks異步生命周期,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • NodeJS學習筆記之Connect中間件模塊(二)

    NodeJS學習筆記之Connect中間件模塊(二)

    本文續(xù)上文的內(nèi)容,介紹下nodejs中connect中間件的使用方式及用途,希望大家喜歡。
    2015-01-01
  • Mongoose學習全面理解(推薦)

    Mongoose學習全面理解(推薦)

    本篇文章主要介紹了Mongoose全面理解,詳細的介紹了mongoose連接數(shù)據(jù)庫,查找讀取數(shù)據(jù)和數(shù)據(jù)驗證等,有興趣的可以了解一下。
    2017-01-01
  • 詳解nodejs微信jssdk后端接口

    詳解nodejs微信jssdk后端接口

    本篇文章主要介紹了詳解nodejs微信jssdk后端接口,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • KOA+egg.js集成kafka消息隊列的示例

    KOA+egg.js集成kafka消息隊列的示例

    這篇文章主要介紹了KOA+egg.js集成kafka消息隊列的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 詳解Node.js實現(xiàn)301、302重定向服務

    詳解Node.js實現(xiàn)301、302重定向服務

    這篇文章主要介紹了詳解Node.js實現(xiàn)301、302重定向服務,詳細的介紹了用Nodejs的http模塊,實現(xiàn)一個301或302重定服務。
    2017-04-04
  • 詳解node.js 事件循環(huán)

    詳解node.js 事件循環(huán)

    這篇文章主要介紹了node.js 事件循環(huán)的相關(guān)資料,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07

最新評論

多伦县| 柯坪县| 玉溪市| 丰镇市| 龙门县| 淳化县| 天祝| 奈曼旗| 乌拉特前旗| 辉南县| 锡林郭勒盟| 巴南区| 通江县| 桐庐县| 聂荣县| 石景山区| 迭部县| 黄山市| 桃园县| 泸水县| 衡南县| 兴仁县| 嘉峪关市| 宿松县| 长宁县| 马公市| 盐池县| 安龙县| 安岳县| 辉县市| 新巴尔虎左旗| 故城县| 灵武市| 迭部县| 龙州县| 昌邑市| 彰武县| 民乐县| 华池县| 安庆市| 四川省|