Get domain name without express.js - node.js

I have a MEAN app which responds to multiple domains. Domains are pointed to same IP and port.
Is there a way to get domain name from client request, without express.js?
So that I can load different database based on request domain name. I tried using process.domain and os.hostname which does not work on Windows OS (not sure about other OS).

Try picking it out from the header:
request.headers.host

Related

One domain (www.example.com) different paths (/ vs. /server2) go to different servers?

I'm wondering if it's possible to host an application on one server (S1) with the root directory of a domain (example.com/) as its approot, but have a TOTALLY DIFFERENT application on another server (S2) with its approot pointing to a specific path of the same domain (example.com/server2).
I'm not talking about subdomains; I'm not talking about load-balancing one application. I'm talking about example.com/* pages on S1, and example.com/server2/* pages on S2. Anyone have any ideas? Thanks!
I'm not familiar with the term "approot", but suspect it is the name of some configuration at the webserver end. It's better to think about these kinds of question from the other end: how will a message get from the browser to the right server. The user gives the browser a URL; the hostname in the URL is looked up in DNS to find an IP address; an HTTP request is sent to that IP address, with the requested path in it.
So ultimately, you have to have some server (or set of interchangeable servers) serving the whole of example.com, because that's the only part that will be looked up in DNS.
That server could, however, be a proxy, which looks at each request it receives, and passes it on to another server based on some configured rules.

Custom Domains for a Simple Web App

I've created a really simple databaseless php application that I want to offer as a hosted solution. I've enable wildcard subdomains so that users can sign up and create a subdomain (e.g. "user.myapp.com"). However, I would like to offer the ability for my users to use a custom domain as well if they prefer. I'm pretty sure this can be accomplished by asking the user to add an A Record to their custom domain pointing to my server's IP, but I'm not sure how to handle the domain on my end once they create an A Record pointing to my server's IP.
So, say a user signs up for my service under "user.myapp.com" and then they decide they want to use a custom domain "someuser.com" instead. My specific question is — once the user adds an A Record to their domain "someuser.com" pointing to my server's IP, how do I tell my server to point that domain to "user.myapp.com"? Or is there an easier way to do this?
You'd need to first setup your server to accept requests from someuser.com, which is entirely different than setting up a wildcard for your server alias (e.g. *.myapp.com). You can have a default vhost handle all the hostnames that no other vhost is setup to handle, but then you're still left with mapping someuser.com to user.myapp.com.
Depending on how you've setup your php application, the user's going to need to enter the custom domain they've registered that they had point to your app, then you'll need to know to do that mapping internally by checking the $_SERVER['HTTP_HOST'] server variable to see what host the request is for, and if it's for someuser.com, then map it to user.myapp.com.

What is exactly cross domain call?

I know if you make a ajax call from example.com to yahoo.com, it is a cross domain call. But I'm not sure about the followings situations.
1) One web application in IIS with domain name "www.test.com" and I also host a web service in IIS with domain name "api.test.com". Now if I make a ajax call from "www.test.com" to "api.test.com", will this be considered as a cross domain call?
2) Same as case 1 but if I host web application ("www.test.com") on a windows server and if I host host web Service ("api.test.com") in a Linux server, and now if I make a ajax call from "www.test.com" to "api.test.com", will this be considered as a cross domain call?
You can not do cross domain calls using javascript. This also includes calls between subdomains.
However getting a work around to make call to a subdomain easy. You need to set same document.domain in both the pages. You can read more about it here A question about cross-domain (subdomain) ajax request
Update:
Forgot to mention that cross domains are independent of backend server serving your requests. So it doesn't matter if server is IIS or Linux, it is all same.

Custom Subdomain with CNAME Problem

I am having a bit of a struggle grasping how to use custom domains with my app. Its the common case of having an app that assigns users to subdomains, ex. user.theapp.com and they want to use a CNAME so m.theirsite.com resolves to the application. It seems that most services that do this require you to tell them what your custom domain is, and that just adding a CNAME record doesn't work. Steps:
User creates an account.
We tell them they can make a CNAME entry to yourstuff.theapp.com (which is the current location).
This is my confusion. After 1&2 my custom domain still isnt working.. so once the client makes that CNAME record and provides us with "m.theirsite.com", what special magic do we do with it to make those sites "the same"?
Thank you in advance.
Our solution was to use PHP/MySQL to solve this. As normal, you should have the external domain/subdomain CNAME'd to your app, however as you will see, the CNAME entry doesn't need to be to the exact subdomain on the app. Next, you will build an area into your database where a user can tell you what external site they have CNAME'd from. At this point, you will perform most of your authentication on the website based on the HTTP host, either grabbing the subdomain and using it as a client, or checking if the HTTP host is in your list of CNAME's and then referencing the client from there.
What the CNAME does is just point to a server location, so if you are using wildcards in your apache configuration, foo.myapp.com resolves to the same location as bar.myapp.com, but in the app can use the host to pull out the subdomain and find the client ("foo" and "bar"). When using a CNAME, like m.mywebsite.com --cnamed--> foo.myapp.com, the application no longer has that client information in the HTTP host, and as we mentioned, the apache wildcard setup (*.myapp.com) just tosses out the subdomain.. so because of this the client must tell us "I will be visiting from m.mywebsite.com, so make that a valid host name for my authentication as well."

Preferred way to direct user's domain names to my web app?

Background context: ASP.NET / IIS (not sure if it matters)
I have a web app at example.com, and a user of my app gets his own content page at an address like example.com/abc-trinkets. I would like to offer the user the ability to point his own registered domain at my web app so his content is accessed at abctrinkets.com. Initially looking on the order of 10-100 users with custom domains.
Ideally, I would like my user to just have a single hostname or IP address that he needs to know to configure properly with his registrar, and if I change the setup of my servers (different host, change addresses, load balancing, etc.) the user will not have to change his settings.
I have no trouble handling the requests once they hit my web app, but I am looking for input on the best way to set the routing up so requests actually come to my app/server. I would like a "catch-all" type of behavior that does not require me to individually configure anything for each domain a user might point to me.
I assume I will need some kind of layer between the address I give my user and my actual server ... is this like a managed DNS service or some other type of nameserver thing I would set up with my host? Is this something simple that should already be handled by a few simple settings on my webserver? I worry that I am making this more complicated than it needs to be.
Write a script that examines the Host header in the request. In your example, if it's abctrinkets.com, then you'd either redirect or forward the request to /abc-trinkets. You'd still need a database or something for mapping the domain names to the URLs; if you're going to allow arbitrary domain names for each user account, then there's no possible way to avoid that.

Resources