nodejs自帶http模組學習

2021-10-06 07:31:47 字數 2539 閱讀 7499

node.js 入門教程

預設啟動埠是3000

根據檔案型別,瀏覽器正確處理資料

如果獲取不到檔案,則返回400錯誤,並跳轉到404.html

如果是進入 / 根目錄,則進入 public/index.html檔案

nodejs檔案只讀取一次

server.js 檔案的啟動檔案

// 進入 404 介面

function

go404page

(res)

res.

writehead

(404,)

; res.

end(data);}

);}// 1. 建立伺服器,得到 server 例項

;// 2. 監聽客戶端的 request 請求事件,設定請求處理函式

// req 請求物件(獲取客戶端資訊)

// res 響應物件(傳送響應資料)

// end() 方法

);// 3. 繫結埠號,啟動伺服器

// 真正需要通訊的應用程式

// 如何從 a 計算機的 應用程式 通訊到 b 計算機的 應用程式

// ip 位址用來定位具體的計算機

// port 埠號用來定位具體的應用程式

// 聯網通訊的應用程式必須占用乙個埠號,同一時間同乙個埠號只能被乙個應用程式占用

// 開發測試的時候使用一些非預設埠,防止衝突

server.

listen

(3000

,function()

);parent.js

const son =

require

('./son'

)son.

getsonname()

console.

log(

'parent init'

)function

getparentname()

module.exports =

;

son.js

console.

log(

'son init'

)function

getsonname()

module.exports =

;

nodejs模組 http模組

處理url請求 var fs require fs 主頁function home res res.write content res.end about.html 關於頁面 function about res res.write content res.end 匯出頁面處理函式 exports....

NodeJS初識 http模組

get的資訊在位址列,不需要設定請求頭資訊 post的資訊在請求體,需要設定請求頭資訊ajax步驟 寫請求頭資訊 訪問位址 傳輸資料 訪問方式http協議 1.建立伺服器 2.req傳送請求體 傳送方式,訪問位址,是否非同步 是否設定請求頭資訊 是否向伺服器傳送資料 post才傳送資料 狀態事件暫時...

nodejs核心模組之http

http模組包含以下5個核心類和方法及屬性 核心類 2.http.createserver 該方法返回http.server類的例項,該例項繼承了eventemitter 類。eventemitter 類有乙個特殊的事件error,當連線失敗的時候會自動觸發該error事件,因此在建立http伺服器...