1

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' **

1 Answer 1

0

I created code sample which resolve you problem :D Firstly you need to add a couple lines to your /etc/hosts file. Example

127.0.0.1 banana.myapp.dev
127.0.0.1 myapp.dev

And after that try to run this script which I wrote for you:

//connect express
var express = require('express');
var subdomain = require('express-subdomain');
var app = express();

app.use(express.json());

//set sub routing
app.sub_banana = express.Router();
app.use(subdomain('banana', app.sub_banana));

//top level routing
app.get('/', (req, res) => {
  res.send('I am Apple')
});

app.get('/about', (req, res) => {
  res.send('Apples don\'t taste good.')
});

//subdomain routing
app.sub_banana.get('/', (req, res) => {
  res.send('I am Banana')
});

app.sub_banana.get('/about', (req, res) => {
  res.send('Apples don\'t taste good.  ** not Bananas are delicious. **')
});

//start server
var http = require('http');

var port = 3000
app.set('port', port);

var server = http.createServer(app);

server.listen(port);

Let me know if you need more information. I got my answer from this resource https://exceed-team.com/tech/express-subdomain

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.