如何在 node(.js)上建立 HTTPS How to build HTTPS connection on Node(.js)
大病初癒,大概確認研究手法後,終於又可以開始寫程式了... (茶 今天想要加點東西,想到由於現在的網路攻擊頻繁,HTTPS已變成連線的主流,於是想試試如何在 node(.js) 上建立 HTTPS連線。 根據 node 官方文件 說明顯示,僅需要三個步驟: 1: var https = require('https'), 2: options = { 3: key: fs.readFileSync('./path/keys/agent-key.pem'), 4: cert: fs.readFileSync('./path/keys/agent-key-cert.pem') 5: }; 6: var serv = https.createServer(options, app); Line 1: 引用https library Line 2: 讀取 pem certificate 跟 key Line 3: 加入 createServer 參數 實際在實作過程中發生了些問題,這裏記錄一下。 問題一:無法連上HTTPS。 一開始利用 curl -k https://localhost:7878/ curl: (35) Server aborted the SSL handshake 發生了 "server aborted the SSL handshake" 發現產生的 cert.pem 是空的,於是發生此錯誤。 PS. server 端則是出現 exception:Missing PFX or certificate + private key 。 問題二:如何產生pem 參考 StackOverFlow: create HTTPS server with Node.js ,如下的 Line 1 跟 Line 3. 1: openssl genrsa 1024 > key.pem 2: WARNING: can't open config file: /opt/anaconda1anaconda2anaconda3/ssl/openssl.cnf 3: openssl...