Michael Joseph Aubry
Michael Joseph Aubry

Reputation: 13492

Node.js subdomain not working?

I am not sure what I am doing wrong, I am trying to do requests from a virtual subdomain using npm subdomain. I followed the directions https://github.com/edwardhotchkiss/subdomain

var subdomain = require('subdomain');
var express  = require('express');
var app      = express();
var http     = require('http');
var server   = http.createServer(app);

app.use(subdomain({ base : 'localhost', removeWWW : true }));

app.get('/subdomain/blog/', function(request, response) {
  response.end('BLOG.LOCALHOST: "/"');
});

app.get('/', function(request, response) {
  response.end('LOCALHOST: "/"');
});

And in my computers hosts file I added

127.0.0.1   localhost
127.0.0.1   blog.localhost

When I route to http://blog.localhost:3000/ I get this returned LOCALHOST: "/" instead of BLOG.LOCALHOST: "/"

I can't figure out why, I removed my express router, I tried removing any routing logic and stuff but I still keep getting that. Obviously I am doing something wrong. What am I missing?

Upvotes: 1

Views: 1132

Answers (1)

stateless
stateless

Reputation: 246

So I see they pass in the require subdomain into app.use, followed by the params. If I reformat their example to look more like your code:

app.use(require('subdomain')({base: 'yourdomain.com'}));

Note they don't pass in anything to the require subdomain. Instead they pass in the JS object which includes the base AFTER they close the require.

Right now your code looks like this.

app.use(subdomain({ base : 'localhost', removeWWW : true }));

So instead try this:

app.use(subdomain()({ base : 'localhost', removeWWW : true }));

OR this:

app.use(require('subdomain')({ base : 'localhost', removeWWW : true }));

And let me know if this fixes your issue.

Upvotes: 1

Related Questions