Subdomains in nodes

How can I handle subdomain requests using nodejs?

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

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

now I want to answer any request http: // [anything] .localhost: 9876 / [anything] this can be done using htaccess in apache, what is the alternative in NodeJS?

+4
source share
1 answer

Your application can already process requests for multiple hosts.

Since you did not specify hostname :

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

And you can determine which host was used for the request from headers :

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

However, you will also need to configure the IP address for subdomains in DNS or hosts .

Or using services like xip.io - using the IP address, not 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/ 
+4
source

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


All Articles