0

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.

Noman Amir
  • 933
  • 3
  • 15
  • 30

3 Answers3

0

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

Community
  • 1
  • 1
dark_ruby
  • 7,646
  • 7
  • 32
  • 57
  • Thanks for the comment ArtDeineka. The thing is, I have seen people using node itself for https purposes, So I wanted to learn how to do it, or what I am doing wrong. Ofcourse, Apache/ngnix is very much useful for this purpose. – Noman Amir Nov 06 '12 at 13:47
  • Thanks for providing the link, I have seen that and tried few of the solutions posted there. I think my main problem is where to put this part of my current server.js : new myApp({port: 8000 }); If you could give me pointer for that, or sample, that would be life-saver! – Noman Amir Nov 06 '12 at 13:55
0

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.

  1. Start by getting it to run on a 'priviledged' port, i.e. change your 8000 to 80, but keep the http server.
  2. Once you've figured out how to run a server with the right priviledges, it should be easy to swap 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.

Community
  • 1
  • 1
rdrey
  • 9,379
  • 4
  • 40
  • 52
  • I am able to run it on port 80/http by changing the port from 8000 to 80. I will look into the link you have provided for express. Thanks! – Noman Amir Nov 06 '12 at 14:10
0

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.

Noman Amir
  • 933
  • 3
  • 15
  • 30