admin管理员组文章数量:1405392
Following another SO question, the latest I have been trying is (see ligatures):
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 the Openshift logs, I get:
Property 'secure' of object # is not a function
This is the line: if ( !req.secure() ) {
The certificates are loaded in the console. The application starts successfully on port 8080.
Why am I getting this message and how should I create a secured Express 4.0 https application in OpenShift? Does anyone have operational code to share? Thanks!
UPDATE
I have updated the redirection as following:
// 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 the following from the console:
SERVER no redirect: http://undefined/
Protocol: http
Host :
Url : /
and my application still does not work. It looks like a bug to me.
I have opened an issue: .cgi?id=1138137
I am also behind Cloudflare, which may be part of the issue.
Following another SO question, the latest I have been trying is (see ligatures):
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 the Openshift logs, I get:
Property 'secure' of object # is not a function
This is the line: if ( !req.secure() ) {
The certificates are loaded in the console. The application starts successfully on port 8080.
Why am I getting this message and how should I create a secured Express 4.0 https application in OpenShift? Does anyone have operational code to share? Thanks!
UPDATE
I have updated the redirection as following:
// 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 the following from the console:
SERVER no redirect: http://undefined/
Protocol: http
Host :
Url : /
and my application still does not work. It looks like a bug to me.
I have opened an issue: https://bugzilla.redhat./show_bug.cgi?id=1138137
I am also behind Cloudflare, which may be part of the issue.
Share Improve this question edited May 23, 2017 at 11:44 CommunityBot 11 silver badge asked Sep 3, 2014 at 22:46 Jérôme VerstryngeJérôme Verstrynge 59.7k96 gold badges295 silver badges466 bronze badges3 Answers
Reset to default 4There were two issues:
i) If you are on Cloudflare's free plan, then it won't forward the SSL requests. If you disactivate Cloudflare (while keeping their DNS for example), then the SSL requests are forwarded to Openshift.
ii) For some unknown reason, some empty requests reach my application on Openshift when starting it, and when browsing pages later too:
req.path: /
req.protocol: http
req.originalUrl: /
req.secure: false
The code mentioned in Openshift's KB article cannot work well as is, for two reasons. First, req.path
does not take query parameters into account. One should use req.originalUrl
instead in Express 4.0.
Second instead of using self.app.get(r, redirectSec, self.routes[r]);
, one should add the middleware with self.app.use
in Express 4.0:
My HTTP to 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();
}
}
});
All seems to work fine now.
req.secure
is a property, not a function.
Try this bit of code from the help.openshift. website:
function redirectSec(req, res, next) {
if (req.headers['x-forwarded-proto'] == 'http') {
res.redirect('https://' + req.headers.host + req.path);
} else {
return next();
}
}
Which can be found in this KB article: https://help.openshift./hc/en-us/articles/202398810-How-to-redirect-traffic-to-HTTPS-
本文标签:
版权声明:本文标题:javascript - Creating an Express JS 4.0 application with https on Openshift, including http redirection - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744892425a2630863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论