Ask coding questions

← Back to all posts
Is there a way to add a dynamic subdomain?
h
programmeruser (615)

Is there a way to add a dynamic subdomain?

Comments
hotnewtop
ch1ck3n (2388)

not in replit

but if youre hosting on your own machine

app.get('/', function (req, res) { 
    var domain = req.get('host').match(/\w+/); // e.g., host: "subdomain.website.com"
    // the regex /\w+/ gets the subdomain eg. "fat.cow.com" "fat"
    if (domain)
       var subdomain = domain[0]; // Use "subdomain" | [0] because the expression returns an array and we want to get the first subdomain
    // now your code, where the dynamic subdomain is stored in the variable "subdomain"
});
programmeruser (615)

@ch1ck3n thanks for the tip, but I don't feel like hosting on my own pc. I'll try glitch

ch1ck3n (2388)

@programmeruser well then you cant

unless you can ask in feedback to add support for subdomains

https://replit.canny.io/hacker-board