Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sub domains in nodejs

How can I handle subdomains request by nodejs?

for example the following code echo test in console for any request on http://localhost:9876/[anything] :

var http = require('http');

http.createServer(function(req, res){
    console.log("test");
}).listen(9876);

now I wanna answer any request on http://[anything].localhost:9876/[anything] it can be done by htaccess in apache,what is alternative in NodeJS?

like image 527
Moein Hosseini Avatar asked Nov 03 '12 10:11

Moein Hosseini


People also ask

Can you have 2 sub domains?

A subdomain is, as the name would suggest, an additional section of your main domain name. You create subdomains to help organize and navigate to different sections of your main website. Within your main domain, you can have as many subdomains as necessary to get to all of the different pages of your website.

Why do we use sub domain?

A subdomain name is a piece of additional information added to the beginning of a website's domain name. It allows websites to separate and organize content for a specific function — such as a blog or an online store — from the rest of your website.

Can we create sub domain?

Rather than registering a new domain name, you can always create a subdomain using your existing domain name. A subdomain is an addon to your primary domain with its unique content. It is a separate part of your website that operates under the same primary domain name without you purchasing a new domain.


1 Answers

Your application is already capable of handling requests for multiple hosts.

Since you haven't specified a hostname:

[...] the server will accept connections directed to any IPv4 address (INADDR_ANY).

And, you can determine which host was used to make the request from the headers:

var host = req.headers.host; // "sub.domain:9876", etc.

Though, you'll also need to configured an IP address for the subdomains in DNS or hosts.

Or, with a services such as xip.io -- using an IP address rather than localhost:

http://127.0.0.1.xip.io:9876/
http://foo.127.0.0.1.xip.io:9876/
http://anything.127.0.0.1.xip.io:9876/
like image 169
Jonathan Lonowski Avatar answered Oct 13 '22 05:10

Jonathan Lonowski