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?
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With