1

I am trying to use Express as my server in MEAN stack on AWS. However I've ran into a issue trying to setup subdomains. I have my main domain name domain.com and id like to have app.domain.com.. However I've tried everything I've found online to write the functionality into the server.js file and nothing works. How can I easily accomplish this to make the second function instead of loading on Port 8000 load up at my app.domain.com sub domain? Thanks in advance!

var express = require('express');
// Main Website
var web = express();
web.get('/', function (req, res){
    res.sendFile('/web/index.html', { root: '.' })
});

var port = 9000;
web.listen(port);
console.log('Web Listening on port', port);

//Main Application
var app = express();
app.get('/', function (req, res){
    res.sendFile('/app/index.html', { root: '.' })
});

var port = 8000;
app.listen(port);
console.log('Web Listening on port', port);

Update:

I tried using Vhost here but it loads the same thing for both the main domain and the sub domain and so it does not work. here is the code I used:

var express = require('express');
var connect = require('connect');
var http = require('http');
var vhost = require('vhost');

// Main Website
var web = express();
web.get('/', function (req, res){
    res.sendFile('/web/index.html', { root: '.' })
});

var port = 9000;
web.listen(port);
console.log('Web Listening on port', port);

//Main Application
var app = connect()

app.use(vhost('app.domain.com', function (req, res) {
      res.sendFile('/app/index.html', { root: '.' })

  httpServer.emit('request', req, res)
}))

app.listen(8000)

I don't really need these to be on separate ports that was just something I was trying originally. But either way does not work still..

Robert
  • 343
  • 6
  • 16
  • Did you configure DNS for `app.domain.com`? If so, did you point it at the same host as `domain.com`? If so, then you will need a proxy like nginx to redirect `app.domain.com` requests to port 8000 unless you want to use the port number in the URL such as `http://app.domain.com:8000`. – jfriend00 Apr 26 '18 at 02:40
  • It's called vhosting (virtual hosting). It used to be part of express, but was moved into it's own module: https://github.com/expressjs/vhost – generalhenry Apr 26 '18 at 02:54
  • I've seen vhost but am unsure how to write my server.js file to use it really. – Robert Apr 26 '18 at 03:14
  • @Robert I provided a way to have 2 express apps on the same instance, and serve each one from a different domain. – Marcos Casagrande Apr 26 '18 at 03:51
  • Hi Marcos, I saw that and thank you. I might try that tomorrow morning. I just would have hoped this would be very simply to do in express by itself.. – Robert Apr 26 '18 at 03:53
  • As @jfriend00 said, unless you want to use the port number in the URL, you will need a proxy / load balancer behind express. – Marcos Casagrande Apr 26 '18 at 03:55
  • alright. I will try to setup the ALB in the morning. I took a look at it but it seems like it once me to go through a lot of steps to set one up. – Robert Apr 26 '18 at 03:57
  • It might seem a lot of steps, but once you know what to do, it will take less done 5 minutes :). And let me know if you need any help. – Marcos Casagrande Apr 26 '18 at 03:59
  • Do I create a application or network load balancer? – Robert Apr 26 '18 at 04:27
  • also do I add those two ports to the listeners when creating the LB? – Robert Apr 26 '18 at 04:29
  • yeah im not seeing the same screen shot you uploaded below. I'm using EC2 instances – Robert Apr 26 '18 at 04:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169885/discussion-between-marcos-casagrande-and-robert). – Marcos Casagrande Apr 26 '18 at 17:19

3 Answers3

4

There's no need for any thing outside of node.js serving on one port. It's just a matter of routing based on the http header.

var express = require('express');
var http = require('http');
var vhost = require('vhost');

// Main Website
var webapp = express();
webapp.get('/', function (req, res){
    res.sendFile('/web/index.html', { root: '.' });
});

//Main Application
var mainapp = express();

mainapp.use(function (req, res) {
    res.sendFile('/app/index.html', { root: '.' });
}));


//Virtual Routing Application

var app = express();

app.use(vhost('app.domain.com', webapp));
app.use(vhost('domain.com', mainapp));
app.use(vhost('www.domain.com', mainapp));

app.listen(9000);
Dev01
  • 13,292
  • 19
  • 70
  • 124
generalhenry
  • 17,227
  • 4
  • 48
  • 63
  • How do you deal with serving static files? – Kevin Toet Sep 03 '19 at 13:05
  • 2
    For static files: https://expressjs.com/en/starter/static-files.html in this case something like `mainapp.use(express.static('public_main'))`, `webapp.use(express.static('public_app'))` – generalhenry Sep 03 '19 at 20:59
1

Since you're using AWS, you can use Application Load Balancer to achieve your goals.

Setup an ALB, and point both domains to the ALB CNAME.

Then you will need to create 2 target groups, one for app.domain.com and another one for domain.com.

Target Group: App

protocol: HTTP
port: 8000

Target Group: Web

protocol: HTTP
port: 9000

Attach your EC2 instance to both target groups

Target group > Targets > Edit > Add to registered

Finally you will have to add an HTTP listener to your ALB, and setup the rules to forward each domain to its target group.

enter image description here

enter image description here

After the rules are set, when you enter to app.domain.com the ALB will forward the request to your express app listening on port 8000, and when browsing domain.com the one listening on port 9000 will be used.

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
0

If you are using Route53 I recommend you to use Records Set to redirect both domain and subdomain to the same ec2 instance via the ip address then you can use ngix like the comments say in this post enter link description here

Using the load balancer (ALB) generates montly the minimum cost of 21.96USD but using Route53 it is minimum of 1USD or less.

If you dont want to use a proxy like nginx you can have s3 website hosting with a low cost, arround 6USD and the route 53 routing by 1USD minimum per month and if you have REST API services you can call from front end to your instance depending the site accessed with no problems.

Regards,

Hector Martinez
  • 417
  • 4
  • 7