简体   繁体   中英

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 version: 0.12.4, request version: 2.55.0

1000-2000 requests can be executed successfully, but then i'm getting such error:

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. 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.

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. Notice how we use https and http depending on the connection type.

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(`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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM