首頁 > web前端 > js教程 > 主體

淺析Nodejs中的http模組和導出共享

青灯夜游
發布: 2022-11-10 20:46:22
轉載
1241 人瀏覽過

這篇文章聊聊node的基礎,關於http模組及module.exports導出共享的理解和案例,希望對大家有幫助!

淺析Nodejs中的http模組和導出共享

一、http 模組

#http 模組是 Node.js 官方提供的、用來建立 web 伺服器的模組。 【相關教學推薦:nodejs影片教學

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

1、建立web 伺服器

  • 導入http 模組
  • 建立web 伺服器實例
  • 為伺服器實例綁定request 事件,監聽客戶端的請求
  • 啟動伺服器

#範例:監聽8080 服務

// 导入 http 模块
const http = require('http')
// 创建 web 服务器实例
const server = http.createServer()
// 为服务器实例绑定 request 事件 监听客户端的请求
server.on('request', function (req, res) {
    console.log('请求中...')
})
// 启动服务
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})
登入後複製

淺析Nodejs中的http模組和導出共享

#2、req 請求物件

只要伺服器接收到了客戶端的請求,就會呼叫透過server.on() 為伺服器綁定的request 事件處理函數

#範例:在事件處理函數中,存取與客戶端相關的資料或屬性

// 导入 http 模块
const http = require('http')
// 创建 web 服务器实例
const server = http.createServer()
// req 是请求对象 包含了与客户端相关的数据和属性
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')
})
登入後複製

淺析Nodejs中的http模組和導出共享

3、res 回應對象

在伺服器的request 事件處理函數中,如果想要存取與伺服器相關的資料或屬性,需要使用response

範例:請求回應

// 导入 http 模块
const http = require('http')
// 创建 web 服务器实例
const server = http.createServer()
// req 是请求对象 包含了与客户端相关的数据和属性
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)
    // 调用 res.end() 方法 向客户端响应一些内容
    res.end(str)
})
// 启动服务
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})
登入後複製

淺析Nodejs中的http模組和導出共享

淺析Nodejs中的http模組和導出共享

4、解決中文亂碼問題

當呼叫res.end() 方法,向客戶端發送中文內容時,會出現亂碼問題,需要手動設定內容的編碼格式

範例:解決中文亂碼

// 导入 http 模块
const http = require('http')
// 创建 web 服务器实例
const server = http.createServer()
// req 是请求对象 包含了与客户端相关的数据和属性
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)
    // 设置 Content-Type 响应头 解决中文乱码问题
    res.setHeader('Content-Type', 'text/html; charset=utf-8')
    // 调用 res.end() 方法 向客户端响应一些内容
    res.end(str)
})
// 启动服务
server.listen(8080, function () {
    console.log('http://127.0.0.1:8080')
})
登入後複製

淺析Nodejs中的http模組和導出共享

淺析Nodejs中的http模組和導出共享

5、根據不同的url 回應不同的html 內容

範例:步驟如下

    # #取得請求的url 位址
  • 設定預設的回應內容為404 Not found
  • #判斷使用者要求的是否為/ 或/index.html 首頁
  • 判斷使用者請求的是否為/about.html 關於頁面
  • 設定Content-Type 回應頭,防止中文亂碼
  • 使用res.end() 把內容回應給客戶端
  • ##
    // 导入 http 模块
    const http = require('http')
    // 创建 web 服务器实例
    const server = http.createServer()
    // req 是请求对象 包含了与客户端相关的数据和属性
    server.on('request', (req, res) => {
        // req.url 客户端请求的 url 地址
        const url = req.url
        // 设置默认的内容为 404 Not Found
        let content = &#39;<h1>404 Not Found!</h1>&#39;
        // 用户请求页是首页
        if(url === &#39;/&#39; || url === &#39;/index.html&#39;) {
            content = &#39;<h1>首页</h1>&#39;
        } else if (url === &#39;/about.html&#39;) {
            content = &#39;<h1>关于页面</h1>&#39;
        }
        // 设置 Content-Type 响应头 防止中文乱码
        res.setHeader(&#39;Content-Type&#39;, &#39;text/html; charset=utf-8&#39;)
        // 调用 res.end() 方法 向客户端响应一些内容
        res.end(content)
    })
    // 启动服务
    server.listen(8080, function () {
        console.log(&#39;http://127.0.0.1:8080&#39;)
    })
    登入後複製

淺析Nodejs中的http模組和導出共享
淺析Nodejs中的http模組和導出共享
淺析Nodejs中的http模組和導出共享
淺析Nodejs中的http模組和導出共享
淺析Nodejs中的http模組和導出共享

#二、Node.js 中的模組分類

1、三大模組分類

內建模組:由node.js 官方提供的,如fs、path、http 等
  • 自訂模組:使用者建立的每個.js 文件,都是自訂模組
  • 第三方模組:由第三方開發出來的模組,使用前要先下載

2、模組作用域防止了全域變數污染的問題

#範例:

index.js 檔案

const username = &#39;张三&#39;

function say() {
    console.log(username);
}
登入後複製

test.js 檔案

const custom = require(&#39;./index&#39;)

console.log(custom)
登入後複製

淺析Nodejs中的http模組和導出共享

#3、module.exports 物件#在自訂模組中,可以使用module.exports 對象,

將模組內的成員共用出去

,供外界使用。 外界require() 方法匯入自訂模組時,得到的就是module.exports 所指向的物件

範例:

index.js 檔案

const blog = &#39;前端杂货铺&#39;

// 向 module.exports 对象上挂载属性
module.exports.username = &#39;李四&#39;
// 向 module.exports 对象上挂载方法
module.exports.sayHello = function () {
    console.log(&#39;Hello!&#39;)
}
module.exports.blog = blog
登入後複製

test.js 檔案

const m = require(&#39;./index&#39;)

console.log(m)
登入後複製

#

4、共享成员时的注意点

使用 require() 方法导入模块时,导入的结果,永远以 module.exports 指向的对象为准

示例:

index.js 文件

module.exports.username = &#39;李四&#39;

module.exports.sayHello = function () {
    console.log(&#39;Hello!&#39;)
}

// 让 module.exports 指向一个新对象
module.exports = {
    nickname: &#39;张三&#39;,
    sayHi() {
        console.log(&#39;Hi!&#39;)
    }
}
登入後複製

test.js 文件

const m = require(&#39;./index&#39;)

console.log(m)
登入後複製

淺析Nodejs中的http模組和導出共享

5、exports 和 module.exports

默认情况下,exports 和 module.exports 指向同一个对象

最终共享的结果,还是以 module.exports 指向的对象为准。

示例:

index1.js 文件

exports.username = &#39;杂货铺&#39;

module.exports = {
    name: &#39;前端杂货铺&#39;,
    age: 21
}
登入後複製

淺析Nodejs中的http模組和導出共享

index2.js 文件

module.exports.username = &#39;zs&#39;

exports = {
    gender: &#39;男&#39;,
    age: 22
}
登入後複製

淺析Nodejs中的http模組和導出共享

index3.js 文件

exports.username = &#39;杂货铺&#39;

module.exports.age = 21
登入後複製

淺析Nodejs中的http模組和導出共享

index4.js 文件

exports = {
    gender: &#39;男&#39;,
    age: 21
}

module.exports = exports

module.exports.username = &#39;zs&#39;
登入後複製

淺析Nodejs中的http模組和導出共享

对 index2.js 文件结果的解析如下:

淺析Nodejs中的http模組和導出共享
对 index4.js 文件结果的解析如下:
淺析Nodejs中的http模組和導出共享
注意:为防止混乱,尽量不要在同一个模块中同时使用 exports 和 module.exports

更多node相关知识,请访问:nodejs 教程

以上是淺析Nodejs中的http模組和導出共享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!