[英]Nodejs Error: Protocol “http:” not supported. Expected “https:”
I am using request module for node.js and time by time i'm getting uncaught error (only on production, maybe after node.js updating)我正在使用 node.js 的请求模块,并且一次又一次地出现未捕获的错误(仅在生产中,可能在 node.js 更新之后)
Node version: 0.12.4, request version: 2.55.0节点版本:0.12.4,请求版本:2.55.0
1000-2000 requests can be executed successfully, but then i'm getting such error:可以成功执行 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)"
How can i fix it?我该如何解决? Why it appears?
为什么会出现? Thanks)
谢谢)
This is caused by running your application using SSL but calling it via normal HTTP.这是由于使用 SSL 运行您的应用程序但通过普通 HTTP 调用它造成的。 You would need to put a check in place to identify if the client is using HTTP or HTTPS and then modify your code to suit.
您需要进行检查以确定客户端是使用 HTTP 还是 HTTPS,然后修改您的代码以适应。
Something like this should work:这样的事情应该工作:
var express = require('express')
, http = require('http')
, https = require('https')
, app = express();
http.createServer(app);
https.createServer({ ... }, app);
Then when you handle your request, do something like然后当你处理你的请求时,做类似的事情
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
identifies if the connection is secure. req.secure
标识连接是否安全。 Notice how we use https
and http
depending on the connection type.请注意我们如何根据连接类型使用
https
和http
。
just FYI, I got a similar error仅供参考,我遇到了类似的错误
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)
which was due to a dumb typo in the url in the fetch instruction这是由于 fetch 指令中 url 中的一个愚蠢的拼写错误
fetch(`https://xxx@yyy.com`, {
method: 'post',
body: JSON.stringify(eventLogEntry),
headers: { 'Content-Type': 'application/json' },
agent: agent
})
my problem went away when I fixed the url to xxx.yyy.com当我将 url 固定到 xxx.yyy.com 时,我的问题就消失了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.