I'm using express-subdomain.
The router that handles requests through a subdomain is the same as the router that handles requests without a subdomain.
I know that my 'app.js' setting is wrong. How can I solve this problem? I want to know a good way. like this:
app.use(subdomain('banana', ('/about', bananaRouter);
and If this is an easy question, please forgive me. I couldn't find any of the same problems in my country. I'm sorry.
// /app.js
const appleRouter = require('./routes/apple/index');
const appleAboutRouter = require('./routes/apple/about');
const applePriceRouter = require('./routes/apple/price');
const bananaRouter = require('./routes/banana/index');
const bananaAboutRouter = require('./routes/banana/about');
const bananaPriceRouter = require('./routes/banana/price');
const grapeRouter = require('./routes/grape/index');
const grapeAboutRouter = require('./routes/grape/about');
const grapePriceRouter = require('./routes/grape/price');
app.use(subdomain('banana', bananaRouter));
app.use(subdomain('grape', grapeRouter));
app.use('/', appleRouter);
app.use('/about', appleAboutRouter);
app.use('/price', applePriceRouter);
app.use('/', bananaRouter);
app.use('/about', bananaAboutRouter);
app.use('/price', bananaPriceRouter);
app.use('/', grapeRouter);
app.use('/about', grapeAboutRouter);
app.use('/price', grapePriceRouter);
// /routes/apple/index
const express = require('express');
const router = express.Router();
router.get('/', function (req, res, next) {
res.send('I am Apple');
});
module.exports = router;
// /routes/apple/about
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("Apples don't taste good.");
});
module.exports = router;
// /routes/apple/price
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("$ 1");
});
module.exports = router;
// /routes/banana/index
const express = require('express');
const router = express.Router();
router.get('/', function (req, res, next) {
res.send('I am Banana');
});
module.exports = router;
// /routes/banana/about
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("Bananas are delicious.");
});
module.exports = router;
// /routes/banana/price
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("$ 2");
});
module.exports = router;
// /routes/grape/index
const express = require('express');
const router = express.Router();
router.get('/', function (req, res, next) {
res.send('I am Grape');
});
module.exports = router;
// /routes/grape/about
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("Grapes are purple.");
});
module.exports = router;
// /routes/grape/price
const express = require('express');
const router = express.Router();
router.get('/view', function (req, res, next) {
res.send("$ 3");
});
module.exports = router;
Expected Behaviour 2:
// http://localhost.com:3000/
I am Apple
// http://localhost.com:3000/about/view
Apples don't taste good.
// http://localhost.com:3000/price/view
$ 1
// http://banana.localhost.com:3000/
I am Banana
// http://banana.localhost.com:3000/about/view
Apples don't taste good. ** not Bananas are delicious. **
// http://banana.localhost.com:3000/price/view
'$ 1' ** not '$ 2' **