node.js+postman實現模擬HTTP伺服器與客戶端互動
目錄
- 一、node搭建HTTP伺服器
- 二、HTTP伺服器處理get請求
- 1、postman傳送get請求
- 2、伺服器解析
- 三、HTTP伺服器處理post請求
- 1、postman傳送post請求
- 2、伺服器解析
當應用程式(客戶端)需要某一個資源時,可以向一臺伺服器通過http請求獲取到這個資源。提供資源的伺服器就是web伺服器(本文用node.建立),用postman模擬客戶端向伺服器傳送請求。
一、node搭建HTTP伺服器
node中使用 http 模組來建立 HTTP 伺服器, 每當接收到新的請求時,request 事件會被呼叫,並提供兩個物件:一個請求req(http.IncomingMessage 物件)和一個響應res(http.ServerResponse 物件)。
request 提供了請求的詳細資訊。 通過它可以訪問請求頭和請求的資料。(客戶端–>伺服器)
response 用於構造要返回給客戶端的資料(伺服器–>客戶端)。下面是一個簡單的HTTP web伺服器示例。
下面就是一個簡單的HTTP伺服器的例子
//匯入http模組 const http = require('http') // 建立http伺服器 const server = http.createServer((req,res) => { //設定 statusCode 屬性為 200,以表明響應成功 res.statusCode = 200 // res本質上繼承了stream.Writable的類 // 向客戶端傳送完響應頭和正文後告訴伺服器本次訊息傳輸結束 res.end("hollow server") // 相當於res.writer("hollow server")+res.end() }) // 監聽伺服器,當伺服器就緒時,listen 回撥函式會被呼叫 //控制檯列印啟動成功 server.listen('8089','localhost',() => { console.log("啟動成功") })
這時候你的本地伺服器就搭建完畢了,可以去瀏覽器開啟localhost:8089檢視
二、HTTP伺服器處理get請求
Postman 是一種常用的介面測試工具,可以傳送幾乎所有型別的HTTP請求。Postman適用於不同的,Postman Mac、Windows X32、Windows X64、系統,還支援postman 瀏覽器擴充套件程式、postman chrome應用程式等。
下載也十分簡單,可以點選這裡直接去官網下載👉👉👉 Download Postman
1、postman傳送get請求
在postman中新建一個request請求,在Enter request url 中填寫上面我們用node.js
http://localhost:8089/login?username=ahua&password=123
,請求型別選擇GET,點選send,postman就會向伺服器傳送一個get請求2、伺服器解析
伺服器這邊接收到客戶端(postman)的get請求,對傳送過來的資料進行處理
const http = require('http') // 處理url的模組 const url = require('url') // 處理query的模組 const qs = require('querystring') const server = new http.Server((req,res) => { // request物件中封裝了客戶端給我們伺服器傳遞過來的所有資訊 // 對urfqXvkdfl進行解析 const { pathname,query } = url.parse(req.url) if (pathname === '/login') { //console.log(query) // qs的parse方法可以對query進行處理 // 將字串型別轉換為js物件 username=ahua&password=123 --> {usernamhttp://www.cppcns.come: 'ahua',password: 123} //console.log(qs.parse(query)) const { username,password } = qs.parse(query) console.log(username,password) res.end('請求結果') } console.log(req.url) //列印請求型別 console.log(req.method) //請求頭 console.log(req.headers) }) server.listen('8089',() => { console.log("serve啟動成功") })
伺服器這邊的解析結果
三、HTTP伺服器處理post請求
1、postman傳送post請求
上面get請求中將使用者名稱和密碼放在位址列可能不夠安全,處理賬號密碼想穩妥一點,現在將他們放在body裡用json檔案傳給伺服器。
下圖所示便是postman將username和password放在json檔案中通過bady傳遞給伺服器的操作
2、伺服器解析
伺服器這邊接收到客戶端(postman)的post請求,對傳送過來的資料進行處理。首先應該判斷是否為post請求,然後拿到body中的資料,再對資料進行解析。
const http = require('http') // 處理url的模組 const url = require('url') const server = new http.Server((req,res) => { // 獲取客戶端傳過來的url中的pathname const { pathname } = url.parse(req.url) // 判斷是否為login if (pathname === '/login') { // 判斷客戶端傳送的是否為post請求 if (req.method === 'POST') { // 定義客戶端傳過來資料的預設編碼格式 req.setEncoding('utf-8') // req.setEncoding('binary') binary定義二進位制編碼 // 拿到body中的資料 // body中的資料是通過流來寫入的 // 當監聽到data事件,獲取到輸入流,也就是body中相關的內容,可以返回這個data的結果 req.on('data',(data) => { // JSON.parse()把物件中的字串轉成js物件 // {"username":"阿花","passward":"123"} ---> {username: 'ahua',password: 123} const { username,passward } = JSON.parse(data) console.log(username,passward) }) } } res.end('請求結果') }) server.listen('8089','localhost',() => { console.log("serve啟動成功www.cppcns.com") })
伺服器列印請求結果
這樣就完成了一個簡單的伺服器互動流程。
到此這篇關於node.js+postman實現模擬HTTP伺服器與客戶端互動的文章就介紹到這了,更多相關node.js+postman 伺服器與客戶端互動內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!