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

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

Open
alexander-daniel opened this issue Aug 25, 2016 · 1 comment

Comments

@alexander-daniel
Copy link

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
Copy link

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

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

2 participants