![](/img/trans.png)
[英]Node js Error: Protocol "https:" not supported. Expected "http:"
[英]Nodejs Error: Protocol “http:” not supported. Expected “https:”
我正在使用 node.js 的請求模塊,並且一次又一次地出現未捕獲的錯誤(僅在生產中,可能在 node.js 更新之后)
節點版本:0.12.4,請求版本:2.55.0
可以成功執行 1000-2000 個請求,但是我收到了這樣的錯誤:
Error: Protocol "http:" not supported. Expected "https:".
at new ClientRequest (_http_client.js:75:11)
at Object.exports.request (http.js:49:10)
at Request.start (/path/node_modules/request/request.js:963:30)
at Request.end (/path/node_modules/request/request.js:1531:10)
at end (/path/node_modules/request/request.js:734:14)
at Immediate._onImmediate (/path/node_modules/request/request.js:748:7)
at processImmediate [as _immediateCallback] (timers.js:358:17)"
我該如何解決? 為什么會出現? 謝謝)
這是由於使用 SSL 運行您的應用程序但通過普通 HTTP 調用它造成的。 您需要進行檢查以確定客戶端是使用 HTTP 還是 HTTPS,然后修改您的代碼以適應。
這樣的事情應該工作:
var express = require('express')
, http = require('http')
, https = require('https')
, app = express();
http.createServer(app);
https.createServer({ ... }, app);
然后當你處理你的請求時,做類似的事情
app.get('/your-route', function(req, res) {
if (req.secure) {
var req = https.get(url, function(res) { ... });
} else {
var req = http.get(url, function(res) { ... });
}
});
req.secure
標識連接是否安全。 請注意我們如何根據連接類型使用https
和http
。
僅供參考,我遇到了類似的錯誤
TypeError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. Expected "https:"
at new ClientRequest (_http_client.js:120:11)
at request (http.js:42:10)
at ..../node_modules/node-fetch/lib/index.js:1432:15
at new Promise (<anonymous>)
at fetch (..../node_modules/node-fetch/lib/index.js:1401:9)
at ClientRequest.<anonymous> (..../node_modules/node-fetch/lib/index.js:1532:15)
at ClientRequest.emit (events.js:198:13)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:565:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:17)
at TLSSocket.socketOnData (_http_client.js:451:20)
at TLSSocket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:94:17)
這是由於 fetch 指令中 url 中的一個愚蠢的拼寫錯誤
fetch(`https://xxx@yyy.com`, {
method: 'post',
body: JSON.stringify(eventLogEntry),
headers: { 'Content-Type': 'application/json' },
agent: agent
})
當我將 url 固定到 xxx.yyy.com 時,我的問題就消失了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.