方法:1、用HTTP模組的「https.get()」方法發出get請求;2、用通用的「https.request()」方法發出post請求;3、用PUT和DELETE請求,只需將“options.method”改為PUT或DELETE即可。
本教學操作環境:windows10系統、nodejs 12.19.0版本、Dell G3電腦。
node怎麼發出https請求
了解Node.js本機HTTPS模組,該模組可以在沒有任何外部依賴的情況下發出HTTP請求。
由於它是本機模組,因此不需要安裝。您可以透過以下程式碼存取它:
const https = require('https');
GET請求
#是一個非常簡單的範例,該範例使用HTTP模組的https.get()方法傳送GET請求:
const https = require('https'); https.get('https://reqres.in/api/users', (res) => { let data = ''; // called when a data chunk is received. res.on('data', (chunk) => { data += chunk; }); // called when the complete response is received. res.on('end', () => { console.log(JSON.parse(data)); }); }).on("error", (err) => { console.log("Error: ", err.message); });
與其他流行的HTTP客戶端收集回應並將其作為字串或JSON物件傳回的方法不同,在這裡,您需要將傳入的資料流連接起來以供以後使用。另一個值得注意的例外是HTTPS模組不支援promise,這是合理的,因為它是一個低階模組並且不是非常用戶友好。
POST請求
要發出POST請求,我們必須使用通用的https.request()方法。沒有可用的速記https.post()方法。
https.request()方法接受兩個參數:
options —它可以是物件文字,字串或URL物件。
callback —回呼函數,用於擷取和處理回應。
讓我們發出POST請求:
const https = require('https'); const data = JSON.stringify({ name: 'John Doe', job: 'DevOps Specialist' }); const options = { protocol: 'https:', hostname: 'reqres.in', port: 443, path: '/api/users', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length } }; const req = https.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { console.log(JSON.parse(data)); }); }).on("error", (err) => { console.log("Error: ", err.message); }); req.write(data); req.end();
options物件中的protocols和`port'屬性是可選的。
PUT和DELETE請求
PUT和DELETE請求格式與POST請求類似。只需將options.method值變更為PUT或DELETE。
這是DELETE請求的範例:
const https = require('https'); const options = { hostname: 'reqres.in', path: '/api/users/2', method: 'DELETE' }; const req = https.request(options, (res) => { // log the status console.log('Status Code:', res.statusCode); }).on("error", (err) => { console.log("Error: ", err.message); }); req.end();
推薦學習:《nodejs影片教學》
以上是node怎麼發出https請求的詳細內容。更多資訊請關注PHP中文網其他相關文章!

React'sstrongCommunityAndecoSystemoffernumerBeneFits:1)age awealthoflibrariesandgithub; 2)AwealthoflibrariesandTools,sustasuicomponentLibontlibemontLibrariesLikeChakaAkraUii; 3)

ReactNativeischosenformobiledevelopmentbecauseitallowsdeveloperstowritecodeonceanddeployitonmultipleplatforms,reducingdevelopmenttimeandcosts.Itoffersnear-nativeperformance,athrivingcommunity,andleveragesexistingwebdevelopmentskills.KeytomasteringRea

在React中正確更新useState()狀態需要理解狀態管理的細節。 1)使用函數式更新來處理異步更新。 2)創建新狀態對像或數組來避免直接修改狀態。 3)使用單一狀態對像管理複雜表單。 4)使用防抖技術優化性能。這些方法能幫助開發者避免常見問題,編寫更robust的React應用。

React的組件化架構通過模塊化、可重用性和可維護性使得可擴展UI開髮變得高效。 1)模塊化允許UI被分解成可獨立開發和測試的組件;2)組件的可重用性在不同項目中節省時間並保持一致性;3)可維護性使問題定位和更新更容易,但需避免組件過度複雜和深度嵌套。

在React中,聲明式編程通過描述UI的期望狀態來簡化UI邏輯。 1)通過定義UI狀態,React會自動處理DOM更新。 2)這種方法使代碼更清晰、易維護。 3)但需要注意狀態管理複雜性和優化重渲染。

TonavigateReact'scomplexecosystemeffectively,understandthetoolsandlibraries,recognizetheirstrengthsandweaknesses,andintegratethemtoenhancedevelopment.StartwithcoreReactconceptsanduseState,thengraduallyintroducemorecomplexsolutionslikeReduxorMobXasnee

RectuseSkeyStoeficelyListifyListIdifyListItemsbyProvidistableIdentityToeachelement.1)keysallowReaeActTotRackChangEsInListSwithouterSwithoutreThoutreTheenteringTheEntirelist.2)selectuniqueandstablekeys,避免使用

KeysinrectarecrucialforOptimizingTherEnderingProcessandManagingDynamicListSefectefection.tospotaTandFixKey與依賴的人:1)adduniqueKeykeystoliquekeystolistItemStoAvoidWarningSwarningSwarningSwarningSperformance和2)useuniqueIdentifiersIdentifiersIdentifiersIdentifiersFromdatainSteAtofIndicessuessuessessemessuessessemessemessemesseysemessekeys,3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

Dreamweaver CS6
視覺化網頁開發工具