欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Node.js中http模塊和導(dǎo)出共享問題

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

一、http 模塊

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

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

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

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

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

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

2、req 請求對象

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

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

// 導(dǎo)入 http 模塊
const http = require('http')
// 創(chuàng)建 web 服務(wù)器實(shí)例
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)
})
// 啟動(dòng)服務(wù)
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})

3、res 響應(yīng)對象

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

示例:請求響應(yīng)

// 導(dǎo)入 http 模塊
const http = require('http')
// 創(chuàng)建 web 服務(wù)器實(shí)例
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() 方法 向客戶端響應(yīng)一些內(nèi)容
    res.end(str)
})
// 啟動(dòng)服務(wù)
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})

4、解決中文亂碼問題

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

示例:解決中文亂碼

// 導(dǎo)入 http 模塊
const http = require('http')
// 創(chuàng)建 web 服務(wù)器實(shí)例
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 響應(yīng)頭 解決中文亂碼問題
    res.setHeader('Content-Type', 'text/html; charset=utf-8')
    // 調(diào)用 res.end() 方法 向客戶端響應(yīng)一些內(nèi)容
    res.end(str)
})
// 啟動(dòng)服務(wù)
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})

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

示例:步驟如下

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

二、Node.js 中的模塊分類

1、三大模塊分類

  • 內(nèi)置模塊:由 node.js 官方提供的,如 fs、path、http 等
  • 自定義模塊:用戶創(chuàng)建的每個(gè) .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() 方法導(dǎo)入自定義模塊時(shí),得到的就是 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、共享成員時(shí)的注意點(diǎn)

使用 require() 方法導(dǎo)入模塊時(shí),導(dǎo)入的結(jié)果,永遠(yuǎn)以 module.exports 指向的對象為準(zhǔn)

示例:

index.js 文件

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

test.js 文件

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

5、exports 和 module.exports

默認(rèn)情況下,exports 和 module.exports 指向同一個(gè)對象

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

示例:

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é)果的解析如下:

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

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

相關(guān)文章

  • node爬取微博的數(shù)據(jù)的簡單封裝庫nodeweibo使用指南

    node爬取微博的數(shù)據(jù)的簡單封裝庫nodeweibo使用指南

    這篇文章主要介紹了node爬取微博的數(shù)據(jù)的簡單封裝庫nodeweibo使用指南,需要的朋友可以參考下
    2015-01-01
  • NodeJS處理Express中異步錯(cuò)誤

    NodeJS處理Express中異步錯(cuò)誤

    本文主要闡述如何在 Express 中使用錯(cuò)誤處理中間件(error-handling middleware)來高效處理異步錯(cuò)誤。在 Github 上有對應(yīng) 代碼實(shí)例 可供參考。
    2017-03-03
  • 如何寫Node.JS版本小游戲

    如何寫Node.JS版本小游戲

    JavaScript的出現(xiàn)催動(dòng)了前端開發(fā)的萌芽,前后端分離促進(jìn)了Vue、React等開發(fā)框架的發(fā)展,Weex、React-Native等的演變賦予了并存多端開發(fā)的能力,而Node.JS的面世無疑是推動(dòng)了Web全棧開發(fā)的步伐。
    2021-05-05
  • node項(xiàng)目使用http模塊發(fā)送get-post請求方式

    node項(xiàng)目使用http模塊發(fā)送get-post請求方式

    這篇文章主要介紹了node項(xiàng)目使用http模塊發(fā)送get-post請求方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • NodeJS、NPM安裝配置步驟(windows版本) 以及環(huán)境變量詳解

    NodeJS、NPM安裝配置步驟(windows版本) 以及環(huán)境變量詳解

    本篇文章主要介紹了NodeJS、NPM安裝配置步驟(windows版本) 以及環(huán)境變量詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 基于Node.js + WebSocket打造即時(shí)聊天程序嗨聊

    基于Node.js + WebSocket打造即時(shí)聊天程序嗨聊

    這篇文章主要介紹了基于Node.js + WebSocket打造即時(shí)聊天程序,有興趣的可以了解一下。
    2016-11-11
  • 新入門node.js必須要知道的概念(必看篇)

    新入門node.js必須要知道的概念(必看篇)

    下面小編就為大家?guī)硪黄氯腴Tnode.js必須要知道的概念(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-08-08
  • Node.js應(yīng)用設(shè)置安全的沙箱環(huán)境

    Node.js應(yīng)用設(shè)置安全的沙箱環(huán)境

    這篇文章主要介紹了Node.js應(yīng)用設(shè)置安全的沙箱環(huán)境的方法以及注意事項(xiàng),對此有需要的朋友可以參考學(xué)習(xí)下。
    2018-04-04
  • NodeJS使用JWT跨域身份驗(yàn)證方案詳解

    NodeJS使用JWT跨域身份驗(yàn)證方案詳解

    JWT是為了在網(wǎng)絡(luò)應(yīng)用環(huán)境間傳遞聲明而執(zhí)行的一種基于JSON的開放標(biāo)準(zhǔn),其組成部分為Header、Payload、Signature.Payload部分才是真正的用戶信息,它是用戶信息經(jīng)過加密之后生成的字符串,Header和Signature是安全性相關(guān)的部分,只是為了保證token的安全性
    2023-02-02
  • 淺析Node.js中的內(nèi)存泄漏問題

    淺析Node.js中的內(nèi)存泄漏問題

    這篇文章主要介紹了淺析Node.js中的內(nèi)存泄漏問題,Node.js是使JavaScript應(yīng)用在服務(wù)器端運(yùn)行的一款框架,需要的朋友可以參考下
    2015-06-06

最新評(píng)論