A.D
A.D

Reputation: 1510

2 node js + express applications sharing https port 443?

I have an ec2 instance where I am hosting a node js application (NodeJS1). I made a alternate version of the node js application (NodeJS2).

I have registered a single domain for the ec2 instance.

I also have ec2 security groups setup so that https uses 443 - There seems to be no way to tell ec2 I'd like https on a custom port.

Since the only possible https port is 443 is there any way I can have both nodejs applications share this port?

EDIT

I should also mention I am using express for my node js app. I am currently starting my app like this:

var app = express();
app.set('port', process.env.PORT || 3030);
fs = require('fs')
var sslOptions = {
                key: fs.readFileSync('./mykey.key'),
                cert: fs.readFileSync('./mycert.crt'),
        };
https = require('https').createServer(sslOptions, app);
var server = https.listen(app.get('port'), function (err) {
if (err) throw err;
console.log('listening on ' + server.address().port)
});

ANOTHER UPDATE

I am now trying this:

var httpProxy = require('http-proxy');
var fs = require('fs');
var proxyTable = {};

proxyTable['example.com/baz'] = 'http://localhost:3030';
proxyTable['example.com/buz'] = 'http://localhost:3031';

var httpOptions = {
    router: proxyTable
};
var httpsOptions = {
    router: proxyTable,
    ssl: {
        key: fs.readFileSync('./mykey.key', 'utf8'),
        cert: fs.readFileSync('./mycert.crt','utf8')}
};
httpProxy.createServer(httpsOptions).listen(443, function(err){
        if (err) throw err;
        console.log('proxy listening...');
});

When I run it i get the following error:

/home/ec2-user/MacaronicWebApp/node_modules/http-proxy/lib/http-proxy/index.js:119
    throw err;
          ^
Error: Must provide a proper URL as target
    at ProxyServer.<anonymous> (/home/ec2-user/MacaronicWebApp/node_modules/http-proxy/lib/http-proxy/index.js:68:35)
    at Server.closure (/home/ec2-user/MacaronicWebApp/node_modules/http-proxy/lib/http-proxy/index.js:125:43)

I searched for this error and it looks like this method is outdated (not supported anymore) how can I get this behavior?

Upvotes: 3

Views: 1247

Answers (1)

Michael Irigoyen
Michael Irigoyen

Reputation: 22947

You could technically run them as two separate apps listening on other ports and then utilize the npm package http-proxy. Using http-proxy, you could serve everything on 443, depending on the requested URI.

For example:

router: {
   'example.com/nodejs1': 'localhost:4000',
   'example.com/nodejs2': 'localhost:4001',
   ...
}

Upvotes: 4

Related Questions