express-subdomain icon indicating copy to clipboard operation
express-subdomain copied to clipboard

Handl "any" subdomain

Open vste23 opened this issue 7 years ago • 7 comments

Hey! I recently discovered this great plugin, but i am facing an issue now. I want to handle "any" subdomain. For example, I need to have sub1.example.com sub2.example.com sub3.example.com and all of them should be handled by one handler. I have tried app.use(subdomain("*", myrouter)) but with no luck. Only possible solution is to defined parent subdomain and catch all sub-sub domains, but it is not what i want to have.

vste23 avatar Jul 09 '17 10:07 vste23

@VALIKCOOL try *.www

bmullan91 avatar Jul 09 '17 15:07 bmullan91

that did the trick. I think it should be documented

juliocanares avatar Jul 15 '17 18:07 juliocanares

@bmullan91 Hmm, for me that does not work... I need to have example.com also served... and when I put *.www as the subdomain, then evrything gets shown the default homepage..

const express = require('express');
const bodyParser = require('body-parser');
const subdomain = require('express-subdomain');

const siteRouter = require('./src/routes/site');

const app = express()

app.use(express.json() );
app.use(express.urlencoded());
app.use(express.static('public'));
app.use(subdomain('*.www', siteRouter));

app.get('/', function(req, res) {
    res.send('Homepage');
});

const server = app.listen(80,'x3.loc', function () {
    var host = server.address().address;
    var port = server.address().port;
    console.log('X3 listening at http://%s:%s', host, port);
});

also, siteRouter file:

const express = require('express');

let router = express.Router();

router.get('/', function(req, res) {
    res.send('Welcome to site');
});

module.exports = router;

randohinn avatar Nov 14 '17 17:11 randohinn

Hi friends i got solution for dynamic domain to work locally:

Use nginx as a server!

If use using apache server, stop that and install nginx 🔥

first add all your domains and subdomains in /etc/hosts: (without this you can't able to make work in local system)

127.0.0.1 github.com 127.0.0.1 sameer.github.com

edit your nginx.conf file: Set a nginx reverse proxy

Add your node js project port, in my project i am using 5000, so i put 5000

server {
        listen 80;
        server_name github.com; 
        location / {
            proxy_pass         http://localhost:5000; 
            proxy_http_version 1.1;
            proxy_set_header Host      $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
    server {
            listen 80;
            server_name *.github.com;
            location / {
                proxy_pass         http://localhost:5000; 
                proxy_http_version 1.1;
                proxy_set_header Host      $host;
                proxy_set_header X-Real-IP $remote_addr;
            }
        }

Instead of app.use(subdomain('yourstuff', router)); // static way Place the below code:

// get your dynamic subdomain
app.use(function(req, res, next) {
  if (!req.subdomains.length || req.subdomains.slice(-1)[0] === 'www') return next();
  // otherwise we have subdomain here
  var subdomain = req.subdomains.slice(-1)[0];
  // keep it
  req.subdomain = subdomain;
  next();
});


// render a page
app.get('/', function(req, res) {
  // no subdomain
  if (!req.subdomain) {
    // render landing page
    res.render('home');
  } else {
    // render subdomain specific data
    res.render('user-page');
  }
});

@VALIKCOOL @randohinn

msameerbm avatar May 28 '18 16:05 msameerbm

@sameercodes - this is exactly what I did! Prety cool, good job for figuring this out as well.

knoxcard avatar Sep 28 '18 11:09 knoxcard

Thanks @knoxcard 😁

msameerbm avatar Sep 29 '18 05:09 msameerbm

how to handle xxx.com (no subdomain)

ghost avatar Aug 28 '21 12:08 ghost