Nodejs error: "http:" protocol is not supported. Expected "https:"
I am using the request module for node.js and from time to time I get an uncaught error (only in production, maybe after node.js update)
Node version: 0.12.4, request version: 2.55.0
1000-2000 requests can succeed, but then I get an error like this:
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 this? Why does this appear? Thank)
source to share
This is caused by starting the application using SSL, but invoking it over normal HTTP. You will need to set a check to determine if the client is using HTTP or HTTPS and then modify your code.
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 process 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
determines if the connection is secure. Please note how we use https
and http
depending on the type of connection.
source to share