久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      Node http模塊學(xué)習(xí):聊聊基本用法

      本篇文章給大家了解一下Node.js http模塊,介紹一下createServer和listen方法,希望對(duì)大家有所幫助!

      Node http模塊學(xué)習(xí):聊聊基本用法

      http 模塊

      使用 Node.js 中創(chuàng)建 Web 服務(wù),主要依賴內(nèi)置的 http 模塊。經(jīng)典的 express.js、koa.js 框架都是以 http 模塊為核心,進(jìn)行的不同程度的封裝。

      創(chuàng)建一個(gè)最簡(jiǎn)單的 Web 服務(wù)只需要幾行代碼。新建一個(gè) index.js 文件,輸入以下內(nèi)容:

      // 1.導(dǎo)入 http 模塊 const http = require('http');  // 2. 調(diào)用 createServer 方法創(chuàng)建服務(wù) const server = http.createServer((request, response) => {     // 3.響應(yīng)給瀏覽器的內(nèi)容 	response.end('Hello, World');  });  // 4.執(zhí)行 listen 方法,啟動(dòng)服務(wù) server.listen(3000, () => {     console.log('服務(wù)器啟動(dòng)成功:http://localhost:3000') })
      登錄后復(fù)制

      然后打開命令行工具,使用 node 命令執(zhí)行該文件:

      node index.js
      登錄后復(fù)制

      命令行工具中會(huì)打印:

      服務(wù)器啟動(dòng)成功:http://localhost:3000
      登錄后復(fù)制

      之后打開瀏覽器,訪問 http://localhost:3000,就能看到以下內(nèi)容:

      Node http模塊學(xué)習(xí):聊聊基本用法

      到這里,只需件簡(jiǎn)簡(jiǎn)單單的 6 行代碼,就創(chuàng)建了一個(gè) web 服務(wù)。【相關(guān)教程推薦:nodejs視頻教程、編程教學(xué)】

      createServer 方法

      可以看到,創(chuàng)建 Web 服務(wù)的核心方法就是 createServer 方法。

      它接收一個(gè)回調(diào)函數(shù),回調(diào)參數(shù)接收兩個(gè)參數(shù),分別是 :

      • request 對(duì)象:表示 HTTP 請(qǐng)求對(duì)象,里面包含了客戶端本次請(qǐng)求攜帶的信息
      • response 對(duì)象:表示 HTTP 響應(yīng)對(duì)象,用于向客戶端設(shè)置響應(yīng)的信息

      這個(gè)回調(diào)函數(shù),就是處理 http 請(qǐng)求,設(shè)置 http 響應(yīng)的主要場(chǎng)所。所以,編寫 web 服務(wù),其實(shí)就是在不停的在這個(gè)函數(shù)中處理請(qǐng)求和響應(yīng)內(nèi)容。這也是基于 HTTP 協(xié)議本身的請(qǐng)求-響應(yīng)模型所決定的。當(dāng)然,實(shí)際開發(fā)中肯定不能這么雜糅的寫,都是使用框架,那會(huì)有很合理的中間件機(jī)制和分層。

      listen 方法

      我們都知道,TCP 協(xié)議是 HTTP 協(xié)議的底層協(xié)議,所有 HTTP 請(qǐng)求的數(shù)據(jù)都是利用 TCP 傳輸?shù)摹?strong>要發(fā)送 HTTP 請(qǐng)求,必須先建立 TCP 連接。

      Node 中要?jiǎng)?chuàng)建 HTTP 服務(wù)也是如此。createServer 方法執(zhí)行后會(huì)創(chuàng)建一個(gè) Server 類的實(shí)例,該 Server 類又繼承自另一個(gè)內(nèi)置模塊 net 中的 Server 類,它身上有一個(gè) listen 方法。下面是相關(guān)的類型聲明,可以理解這其中的關(guān)系。

      // net 模塊  class Server extends EventEmitter {           /**          * 啟動(dòng)服務(wù)器監(jiān)聽連接。此 Server 可以是 TCP 或 IPC 服務(wù)器,具體取決于它所監(jiān)聽的內(nèi)容。          */         // 有若干重載,這是最常使用的一種方法         listen(port?: number, hostname?: string, listeningListener?: () => void): this;         listen(port?: number, listeningListener?: () => void): this; }   // http 模塊 import { Server as NetServer } from 'node:net';  function createServer<         Request extends typeof IncomingMessage = typeof IncomingMessage,         Response extends typeof ServerResponse = typeof ServerResponse,     >(requestListener?: RequestListener<Request, Response>): Server<Request, Response>;        class Server<         Request extends typeof IncomingMessage = typeof IncomingMessage,         Response extends typeof ServerResponse = typeof ServerResponse,     > extends NetServer {}
      登錄后復(fù)制

      所以通過 createServer 方法創(chuàng)建了服務(wù)之后,還用再調(diào)用 listen 方法,用于啟動(dòng)該服務(wù),監(jiān)聽連接。

      它有多種重載,最常用的一種就是監(jiān)聽一個(gè)端口,在啟動(dòng)成功后,執(zhí)行回調(diào)函數(shù):

      server.listen(3000, () => {     console.log('服務(wù)器啟動(dòng)成功:http://localhost:3000') })
      登錄后復(fù)制

      如果服務(wù)啟動(dòng)失敗,比如出現(xiàn)端口占用時(shí),控制臺(tái)中會(huì)直接報(bào)錯(cuò),就不再執(zhí)行后面的回調(diào)函數(shù)了。

      再介紹兩種其他的重載,使用的并不多。

      一種是不指定端口號(hào),此時(shí)操作系統(tǒng)會(huì)隨機(jī)分配一個(gè)可用的端口號(hào)。在服務(wù)啟動(dòng)成功后,可以通過 server.address().port 屬性來獲得分配的端口號(hào):

      server.listen(() => {     const port = server.address().port     console.log(port)     console.log('服務(wù)器啟動(dòng)成功:http://localhost:', port) })
      登錄后復(fù)制

      試一下:

      Node http模塊學(xué)習(xí):聊聊基本用法

      一種是指定要監(jiān)聽的主機(jī)名。

      當(dāng)不指定 host 時(shí),默認(rèn)為 0.0.0.0(IPv4)或者 ::(IPv6),支持網(wǎng)絡(luò)中所有的主機(jī)進(jìn)行訪問

      如果設(shè)置為 locolhost 或者 127.0.0.1,則只能自己的主機(jī)訪問。

      server.listen(3000, 'localhost', () => {     console.log('服務(wù)器啟動(dòng)成功:http://localhost:', port) })
      登錄后復(fù)制

      總結(jié)

      我們介紹了 http 模塊的基本用法,主要使用 createServer 來創(chuàng)建服務(wù),再調(diào)用 listen 方法來啟動(dòng)服務(wù),監(jiān)聽連接。createServer 接收回調(diào)函數(shù)用來具體處理某一個(gè)請(qǐng)求,編寫 web 服務(wù)主要就是利用回調(diào)函數(shù)的兩個(gè)參數(shù) requestresponse ,后面會(huì)詳細(xì)介紹。

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)