简体   繁体   English

Nodejs 错误:不支持协议“http:”。 预期“https:”

[英]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.请注意我们如何根据连接类型使用httpshttp

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.

相关问题 节点 js 错误:不支持协议“https:”。 预期“http:” - Node js Error: Protocol "https:" not supported. Expected "http:" Azure SpeechSynthesizer 错误 HTTP:不支持。 预期的 HTTPS:NPM `microsoft-cognitiveservices-speech-sdk` - Azure SpeechSynthesizer Error HTTP: not supported. Expected HTTPS: NPM `microsoft-cognitiveservices-speech-sdk` AngularJS错误:仅支持协议方案的跨源请求:http,数据,chrome扩展,https - AngularJS Error: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https DiscordAPIError:无效的表单正文 embed.image.url:不支持方案“[对象响应]”。 方案必须是 ('http', 'https') 之一 - DiscordAPIError: Invalid Form Body embed.image.url: Scheme “[object response]” is not supported. Scheme must be one of ('http', 'https') 协议HTTP和https不匹配 - Mismatch protocol http and https CORS 错误:“请求仅支持协议方案:http…”等 - CORS Error: “requests are only supported for protocol schemes: http…” etc 仅协议方案支持React Native WebView Cross起源请求:http,data,chrome,https - React Native WebView Cross origin requests are only supported for protocol schemes: http, data, chrome, https 交叉源请求仅支持协议方案:http,数据,chrome,chrome-extension,https - Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https 将协议从 http 更改为 https 时出现 Google Analytics 错误 - Google Analytics error when changing protocol from http to https Google资料库错误:请求访问的框架具有“https”协议,正在访问的框架具有“http”协议 - Google wallet error: The frame requesting access has a protocol of “https”, the frame being accessed has a protocol of “http”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM