I have a simple application that I run using node.js. Below is my server.js:
var myApp = require('./lib/myapp')
new myApp({
port: 8000
});
I can access the application at http://myserver.com:8000
I would like to set it up, so I can access it using https://myserver.com
I have tried various methods answered in questions asked here, but I am unable to get it working. I think the way application is being initialized is the reason, but I am not sure about that.
I know I can put it behind Apache, but I would like to do it using node.
If anyone can modify this server.js for me, that would be a great help!
Thanks
Noman A.
https
suggests that you want to run it over secure layer (port 443), your best bet is to put it behind apache or better nginx
, and let real web server manage ssl certificates, etc while your node.js concentrates on application business logic
if you want to do it by means of node.js look here
Is myApp an express
/connect
app? I don't like the strange constructor with new
keyword you created, we'd need to see the content of that to help.
8000
to 80
, but keep the http
server. http
for https
and 80 for 443.BUT since your clients will try to reach http
first, you will need to redirect them to the https
version of your site. If you do it "the express way", you can set up the same app and simply make it listen with both an http and an https server. Then add a middleware to redirect all http
requests to https
and you're done. Look at this How to force SSL / https in Express.js.
EDIT: This answer has a cleaner double-server/single-app setup, but doesn't include anything about forwarding requests: Listen on HTTP and HTTPS for a single express app
The ideal setup exports the express/connect app object in main.js
. A separate runner.js
simply requires the app and passes it to two servers (1 http & 1 https) with the correct options.
Thanks everyone for their contribution to this question. I really appreciate all the pointers!
Special mention to JohnnyHK, whose suggestion got me on track. I was able to resolve my issue using node-http-proxy. Simple and effective! Below is how my server.js looks like now:
var MyApp = require('./lib/app');
var fs = require('fs'),
http = require('http'),
https = require('https'),
httpProxy = require('http-proxy');
var options = {
https: {
key: fs.readFileSync('certs/server.key', 'utf8'),
cert: fs.readFileSync('certs/server.crt', 'utf8'),
ca: fs.readFileSync('certs/ca.crt', 'utf8')
},
target: {
https: true // This could also be an Object with key and cert properties
}
};
var app = new MyApp({
port: 8080
});
var proxy = new httpProxy.HttpProxy({
target: {
host: 'localhost',
port: 8080
}
});
https.createServer(options.https, function (req, res) {
proxy.proxyRequest(req, res)
}).listen(443);
Thanks to everyone one again!
-Noman A.