I made a few modifications to the example 'Divide and Conquer' section on the npm docs: https://www.npmjs.com/package/express-subdomain
const subdomain = require('express-subdomain');
const express = require('express');
const app = express();
var router = express.Router(); //main api router
var v1Routes = express.Router();
v1Routes.get('/test', function(req, res) {
res.send('API - version 1');
});
router.use(subdomain('api', v1Routes));
//basic routing..
router.get('/', function(req, res) {
res.send('Welcome to the API!');
});
//attach the api
app.use(subdomain('api', router));
app.listen(8000);
console.log("listening on 8000")
Basically, I got rid of the v2 routes and changed the subdomain on the v1 routes to just api
. I also got rid of the middleware and changed the v1 route to /test
just to differentiate it from the basic routing.
Also my /etc/hosts file looks like this:
127.0.0.1 myapp.dev
127.0.0.1 api.myapp.dev
255.255.255.255 broadcasthost
::1 localhost
The only thing I did there is change localhost
to myapp.dev
and added api.myapp.dev
, which I found here: https://github.com/bmullan91/express-subdomain#developing-locally
Any ideas?