Build an Express JS 4.0 app using https on Openshift, including http redirection

Following another SO question , the last thing I'm trying to do (see ligatures.net ):

self.ipaddress = process.env.OPENSHIFT_NODEJS_IP; self.port = process.env.OPENSHIFT_NODEJS_PORT || 443; if (typeof self.ipaddress === "undefined") { self.ipaddress = "127.0.0.1"; }; ... self.app = express(); // 4.8.7 ... // Trusting Openshift proxy self.app.enable('trust proxy'); // Http -> Https redirection middleware self.app.use(function (req, res, next) { if ( !req.secure() ) { var tmp = 'https://' + process.env["DOMAIN_NAME"] + req.originalUrl; console.log("Redirect to: " + tmp); res.redirect(tmp); } else { next(); } }); ... // Creating a http server https.createServer(self.app).listen(self.port, self.ipaddress, function(err) { if (err) { console.log("Node server error: " + err.toString()); } else { console.log('%s: Node server started on %s:%d ...', Date(Date.now() ), self.ipaddress, self.port); } }); 

In Openshift magazines, I get:

The 'secure' property of object # is not a function

This is the line: if ( !req.secure() ) {

Certificates are uploaded to the console. The application runs successfully on port 8080.

Why am I receiving this message and how do I create a secure htp 4.0 Express application in OpenShift? Does anyone have a transaction code to share? Thanks!

UPDATE

I updated the redirect as follows:

 // Http -> Https redirection middleware self.app.use(function (req, res, next) { if ( req.headers['x-forwarded-proto'] === 'http' ) { var tmp = 'https://' + req.headers.host + req.originalUrl; console.log("SERVER redirect to: " + tmp); res.redirect(tmp); } else { var pt = req.protocol || ""; var ho = req.headers.host || ""; var ur = req.originalUrl || ""; console.log("\nProtocol: " + pt + "\nHost : " + ho + "\nUrl : " + ur); var tmp = req.protocol + '://' + req.headers.host + req.originalUrl; console.log("SERVER no redirect: " + tmp); next(); } 

I see a couple of commands from the console:

 SERVER no redirect: http://undefined/ Protocol: http Host : Url : / 

and my application is still not working. This seems like a mistake.

I discovered the problem: https://bugzilla.redhat.com/show_bug.cgi?id=1138137

I am also for Cloudflare, which may be part of the problem.

+6
source share
3 answers

There were two questions:

i) If you are in a free Cloudflare plan, it will not forward SSL requests. If you disable Cloudflare (for example, maintaining your DNS), SSL requests will be redirected to Openshift.

ii) For some unknown reason, some empty requests reach my Openshift application when I start it, and also when I view pages later:

 req.path: / req.protocol: http req.originalUrl: / req.secure: false 

The code provided in the Openshift KB article cannot work as it is for two reasons. Firstly, req.path does not consider query parameters. In Express 4.0, req.originalUrl should be used.

The second instead of using self.app.get(r, redirectSec, self.routes[r]); add middleware with self.app.use in Express 4.0:

My HTTP-HTTPS forwarding code is now:

 // Trusting Openshift proxy self.app.enable('trust proxy'); // Http -> Https redirection middleware self.app.use(function (req, res, next) { if ( req.headers['x-forwarded-proto'] === 'http' ) { var tmp = 'https://' + req.headers.host + req.originalUrl; res.redirect(tmp); } else { return next(); } } }); 

Everything seems to be working fine.

+4
source

req.secure is a property, not a function.

+3
source

Try this bit of code from help.openshift.com:

 function redirectSec(req, res, next) { if (req.headers['x-forwarded-proto'] == 'http') { res.redirect('https://' + req.headers.host + req.path); } else { return next(); } } 

What can be found in this article in KB: https://help.openshift.com/hc/en-us/articles/202398810-How-to-redirect-traffic-to-HTTPS-

+2
source

Source: https://habr.com/ru/post/974780/


All Articles