Sunday 19 March 2017

Why the subdomains are not working with express.js?

so I am experimenting with it locally, this is in my hosts file

127.0.0.1       example.dev
127.0.0.1       www.example.dev
127.0.0.1       api.example.dev

and this is my code:

var subdomain = require('express-subdomain');
var express = require('express');
var app = express();
var router = express.Router();

// example.com 
app.get('/', function(req, res) {
    res.send('Homepage');
});

//api specific routes 
router.get('/', function(req, res) {
    res.send('Welcome to our API!');
});

router.get('/users', function(req, res) {
    res.json([
        { name: "Brian" }
    ]);
});

app.use(subdomain('api', router));
app.listen(3000);

it's basically the example from the package website api.example.dev/users works well, but when I go to to api.example.dev the content is the same as on example.dev (like it is overwritten) any ideas what I am doing wrong? thanks



via gyula

No comments:

Post a Comment