日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Node.js HTTPS

發布時間:2025/3/14 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Node.js HTTPS 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
穩定性: 3 - 穩定

HTTPS 是基于 TLS/SSL 的 HTTP 協議。在 Node 里作為單獨的模塊來實現。

類: https.Server

這是 tls.Server 的子類,并且和 http.Server 一樣觸發事件。更多信息參見 http.Server 。

server.setTimeout(msecs, callback)

詳情參見 http.Server#setTimeout().

server.timeout

詳情參見 http.Server#timeout.

https.createServer(options[, requestListener])

返回一個新的 HTTPS 服務器對象。其中 options 類似于 [tls.createServer()][tls.md#tls_tls_createserver_options_secureconnectionlistener]。 requestListener 函數自動加到 'request' 事件里。

例如:

// curl -k https://localhost:8000/ var https = require('https'); var fs = require('fs'); var options = { key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem') }; https.createServer(options, function (req, res) { res.writeHead(200); res.end("hello world\n"); }).listen(8000);

var https = require('https'); var fs = require('fs'); var options = { pfx: fs.readFileSync('server.pfx') }; https.createServer(options, function (req, res) { res.writeHead(200); res.end("hello world\n"); }).listen(8000);

server.listen(port[, host][, backlog][, callback])

server.listen(path[, callback])

server.listen(handle[, callback])

詳情參見 http.listen()。

server.close([callback])

詳情參見 http.close()。

https.request(options, callback)

發送請求到安全 web 服務器。

options 可以是一個對象或字符串。如果 options 是字符串。會被 url.parse() 解析。

所有來自 http.request() 選項都是經過驗證的。

例如:

var https = require('https'); var options = { hostname: 'encrypted.google.com', port: 443, path: '/', method: 'GET' }; var req = https.request(options, function(res) { console.log("statusCode: ", res.statusCode); console.log("headers: ", res.headers); res.on('data', function(d) { process.stdout.write(d); }); }); req.end(); req.on('error', function(e) { console.error(e); });

option 參數有以下的值:

  • host: 請求的服務器域名或 IP 地址,默認:'localhost'
  • hostname: 用于支持 url.parse()。 hostname 優于 host
  • port: 遠程服務器端口。 默認: 443.
  • method: 指定 HTTP 請求方法。 默認: 'GET'.
  • path: 請求路徑。 默認: '/'。如果有查詢字符串,則需要包含。比如 '/index.html?page=12'
  • headers: 包含請求頭的對象
  • auth: 用于計算認證頭的基本認證,即user:password
  • agent: 控制Agent的行為。當使用了一個Agent的時候,請求將默認為Connection: keep-alive。可能的值為:
    • undefined (default): 在這個主機和端口上使用 [global Agent][]
    • Agent object: 在Agent中顯式使用 passed.
    • false: 選擇性停用連接池,默認請求為: Connection: close

tls.connect() 的參數也能指定。但是,globalAgent 會忽略他們。

  • pfx: SSL 使用的證書,私鑰,和證書Certificate, 默認 null.
  • key: SSL 使用的私鑰. 默認 null.
  • passphrase: 私鑰或 pfx 的口令字符串. 默認 null.
  • cert: 所用公有 x509 證書. 默認 null.
  • ca: 用于檢查遠程主機的證書頒發機構或包含一系列證書頒發機構的數組。
  • ciphers: 描述要使用或排除的密碼的字符串,格式請參閱http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT
  • rejectUnauthorized: 如為 true 則服務器證書會使用所給 CA 列表驗證。如果驗證失敗則會觸發 error 事件。驗證過程發生于連接層,在 HTTP 請求發送之前。缺省為 true.
  • secureProtocol: 所用的 SSL 方法,比如 TLSv1_method 強制使用 TLS version 1。可取值取決于您安裝的 OpenSSL, 和定義于 SSL_METHODS 的常量。

要指定這些選項,使用一個自定義 Agent.

例如:

var options = {hostname: 'encrypted.google.com', port: 443, path: '/', method: 'GET', key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem') }; options.agent = new https.Agent(options); var req = https.request(options, function(res) { ... }

或者不使用 Agent.

例如:

var options = {hostname: 'encrypted.google.com', port: 443, path: '/', method: 'GET', key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem'), agent: false }; var req = https.request(options, function(res) { ... }

https.get(options, callback)

和 http.get() 類似,不過是 HTTPS 版本的.

options 可以是字符串對象. 如果 options 是字符串, 會自動使用 url.parse() 解析。

例如:

var https = require('https'); https.get('https://encrypted.google.com/', function(res) { console.log("statusCode: ", res.statusCode); console.log("headers: ", res.headers); res.on('data', function(d) { process.stdout.write(d); }); }).on('error', function(e) { console.error(e); });

類: https.Agent

HTTPS 的 Agent 對象,和 http.Agent 類似。 詳情參見 https.request()。

https.globalAgent

所有 HTTPS 客戶端請求的 https.Agent 全局實例。

轉載于:https://www.cnblogs.com/navysummer/p/8458746.html

總結

以上是生活随笔為你收集整理的Node.js HTTPS的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。