-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Https redirection #778
Comments
Create two servers and redirect all requests from one to the other: var Hapi = require('hapi');
var http = new Hapi.Server(80);
var https = new Hapi.Server(443, { tls: {} });
var redirect = function () {
this.reply.redirect('https://your.site/' + this.params.path);
});
http.route({ method: '*', path: '/{path*}', handler: redirect }); Note that this is using current 1.0 rc in master. In previous versions (0.16.0 from npm) use: this.reply.redirect('https://your.site/' + this.params.path).send(); |
Thanks hueniverse for the prompt reply. Makes sense. Cheers Jozz |
How would I do that on heroku or openshift which assign just one port to your app and take care of the whole ssl business themselves? This is my solution so far but it seems rather hacky:
|
@chmanie you can just redirect right from onRequest. |
You mean like this?
|
Yep. |
Thank you, this seems to work except I run into this issue when trying to access the non protected route: hapijs/yar#34 |
@chmanie I have the same issue (single port), where exactly are you putting that code to handle the redirect? |
You basically can put it wherever you have access to the |
@chmanie I did When I enter |
For anyone Googling this, I popped my redirect into a plugin: |
@carlo-m Did you ever find a solution to redirecting HTTP to HTTPS using a single port? I'm experiencing the exact same issue you described. @bendrucker @chmanie I'm using code almost exactly like the examples on this page and the plugin - It works fine for a HTTPS request but the browser just hangs for a HTTP request - any ideas what I'm doing wrong? If you set up TLS on the connection maybe it ignores HTTP requests?
|
@kristiankeane which version of hapi are you using? |
@chmanie I think @kristiankeane solved it with help from the gitter |
Usecase Hapi + Glue + Docker.I made a rather elaborate example, to connect the dots, useful for noobs ;-) You can make an entry in manifest.js, ie.:
Register the plugin(https-redirect) in manifest.js:
Write the plugin(./plugins/https-redirect.js) :
Docker run:
|
I've created a simple hapi plugin for easy https redirections. You can find it here: |
Hello,
I'm using nodejitsu to host my app and I need to redirect all http requests to https at the server level.
https://www.nodejitsu.com/documentation/a-quickstart/faq/#how-do-i-force-my-clients-to-use-https-with-my-application
I'm not quire sure how to do this using hapi. I believe I should use the "onReqest' extension, but that only provides setUrl and setMethod methods. I need to force a redirect at this stage.
Can you please advise the best way to achieve this.
Thanks,
Jozz
The text was updated successfully, but these errors were encountered: