RAM MAKESHWARAN
RAM MAKESHWARAN

Reputation: 41

Angular Subdomain

I am trying to create a subdomain in angular 6 using the below method.

For example:
localhost:4200
client1.localhost:4200
client2.localhost:4200

  getSubdomain() {
    const domain = window.location.hostname;
    if (domain.indexOf('.') < 0 
       || domain.split('.')[0] === 'example' 
       || domain.split('.')[0] === 'lvh' 
       || domain.split('.')[0] === 'www') {
       this.subdomain = '';
    } else {
       this.subdomain = domain.split('.')[0];
    }
    console.log('subdomain', this.subdomain);
  }
  1. When running the command 'ng serve --disable-host-check' it's working fine. But this is not secure way.
  2. 'Invalid Host Header' error message is displayed, when the command 'ng serve' is ran.

Kindly suggest me any other option to create a subdomain in angular 6.

Upvotes: 2

Views: 4408

Answers (1)

vinod beloshe
vinod beloshe

Reputation: 109

For development purpose you can add sub domain in etc/host file (C:\Windows\System32\drivers\etc\hosts) like below.

127.0.0.1       client1
::1         client1
127.0.0.1       client2
::1         client2

Serve application by using command:-

ng serve --host client1 --port 4300 --open
ng serve --host client2 --port 4400 --open

Upvotes: 3

Related Questions