How to route subdomain usage in angular2?

I want to create an Angular 2 application with different user landing pages, for example, "user1.example-domain.com" and "user2.example-domain.com" will have a different and unrelated "home page".

My questions:

  • How do I do this using Angular 2?
  • How to check it on a local machine? is "user1.localhost: port" enough?

I tried using static routing as follows:

{path: 'test.localhost:4200/generic-link1', component: GenericLink1Component} 

I saw how this was done in the toggle switch. I know that this can be done.

+6
source share
1 answer

This is easy to do, but not with Angular (or any other JavaScript router, for that matter). Routing to a subdomain requires a server for assistance. You need to configure user1 and user2 as subdomains, and then into your default document - based on what you are doing.

Then the Angular application will be hosted at one of the addresses, so it will be http://user1.example.com/index.html , and the other will be http://user2.example.com . This may be the same application.

To solve all- Angular, you just have to change your schema to use http://example.com/user1 .

There is another reason why the latest URL scheme might be better for you. If you are the host, you can create subdomains for your heart content. If, however, you use shared hosting, there is a rather high probability that you are limited by the number of subdomains that you can use - this can be just two.

+2
source

Source: https://habr.com/ru/post/1012796/


All Articles