bmullan91 / express-subdomain

Super simple subdomain middleware for expressjs
414 stars 49 forks source link

Requests to non-existent subdomain routes fall-back to app routes. #27

Open alexander-daniel opened 8 years ago

alexander-daniel commented 8 years ago

Hello! Just wanted to say thank you very much for this awesome package. I've been able to get up and running really quickly, and it is so far very pleasing to use!

I am however, running into a little issue. I'm probably doing something dumb, but I figured I'd pose the question here to find out a bit more.

const PORT = 5759;
const subdomain = require('express-subdomain');
const express = require('express');
const app = express();

/*
 * SUBDOMAIN ROUTER `create.example.com/{whatever}`
 */

const router = express.Router();

const creatorRoutes = [
  '',
  'home',
  'profile*'
];

creatorRoutes.forEach(route => {
  router.get(`/${route}`, (req, res) => {
    res.sendFile(`${__dirname}/creator-index.html`);
  });
});

router.get('/creator-login.html', (req, res) => {
  res.sendFile(`${__dirname}/creator-login.html`);
});

app.use(subdomain('create', router));

/*
 * MAIN ROUTES `example.com/{whatever}`
 */
const mainRoutes = [
  '',
  'search'
];

mainRoutes.forEach(route => {
  app.get(`/${route}`, (req, res) => {
    res.sendFile(`${__dirname}/main-index.html`);
  });
});

app.get('/main-login.html', (req, res) => {
  res.sendFile(`${__dirname}/main-login.html`);
});

app.listen(PORT);

Now this works beautifully when you hit:

http://create.example.com/home http://create.example.com/profile

and so on, those work great. They serve up creator-index.html and everything is hunky-dory. Vice versa for the search and "" routes for the main app router.

However http://create.example.com/search redirects to the base, non-subdomained URL, even though it is not definedin my subdomain router. Is there a reason why it falls back to the app routes, rather than seeing, "Oh, I have nothing in the subdomain router that matches that, i should return 404"

Thank you very much for your time and I appreciate any insight you might have!

theswedishdev commented 8 years ago

Hello @alexander-daniel! I found a solution to your issue which is basically to rearrange your code. Instead of defining your main routes last, I would suggest to define them before the subdomain routes. This did the trick for me:

const PORT = 5759;
const subdomain = require('express-subdomain');
const express = require('express');
const app = express();

/*
 * MAIN ROUTES `example.com/{whatever}`
 */
const mainRoutes = [
  '',
  'search'
];

mainRoutes.forEach(route => {
  app.get(`/${route}`, (req, res) => {
    res.sendFile(`${__dirname}/main-index.html`);
  });
});

app.get('/main-login.html', (req, res) => {
  res.sendFile(`${__dirname}/main-login.html`);
});

/*
 * SUBDOMAIN ROUTER `create.example.com/{whatever}`
 */

const router = express.Router();

const creatorRoutes = [
  '',
  'home',
  'profile*'
];

creatorRoutes.forEach(route => {
  router.get(`/${route}`, (req, res) => {
    res.sendFile(`${__dirname}/creator-index.html`);
  });
});

router.get('/creator-login.html', (req, res) => {
  res.sendFile(`${__dirname}/creator-login.html`);
});

app.use(subdomain('create', router));

app.listen(PORT);