今天群里一位同學在做練習的時候,采用https例子:
// curl -k https://localhost:8000/ const https = require('https'); const fs = require('fs'); const options = { key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem') }; https.createServer(options, (req, res) => { res.writeHead(200); res.end('hello world\n'); }).listen(8000);
在瀏覽器輸入localhost:8000,回車,無法正常得到返回的'hello world',控制台也沒有任何報錯,通過瀏覽器的調試控制台我們可以看到:
get請求中的url路徑是http而不是https,這是由於現階段瀏覽器地址欄輸入在沒有明確是https或者http時,默認發送的是http請求,故而得不到響應。
其實在官網API的例子里也有提示這一點:
寫這篇博文是為了讓遇到這個問題的新手能夠快速找到答案。