Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handl "any" subdomain #33

Open
vste23 opened this issue Jul 9, 2017 · 7 comments
Open

Handl "any" subdomain #33

vste23 opened this issue Jul 9, 2017 · 7 comments

Comments

@vste23
Copy link

vste23 commented Jul 9, 2017

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.

@bmullan91
Copy link
Owner

@VALIKCOOL try *.www

@juliocanares
Copy link

that did the trick. I think it should be documented

@randohinn
Copy link

randohinn commented Nov 14, 2017

@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;

@msameerbm
Copy link

msameerbm commented May 28, 2018

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

@knoxcard
Copy link

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

@msameerbm
Copy link

Thanks @knoxcard 😁

@ghost
Copy link

ghost commented Aug 28, 2021

how to handle xxx.com (no subdomain)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants