Mason G. Zhwiti
Mason G. Zhwiti

Reputation: 6540

Limit Node express to specific hostnames?

What's the best way to reject requests to my web server (running via Node express) that are coming in to an unrecognized hostname? I.e. I only want to respond to requests that are intended for my domain, not for requests that are just aimed at my IP address.

Upvotes: 1

Views: 144

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161457

The easiest way would probably be to use the connect vhost middleware.

Where you would normally do this:

var app = express();
app.get('/', function(req, res){
   res.send('HI');
});

app.listen(80);

You would do this:

var vhostApp = express();
vhostApp.get('/', function(req, res){
   res.send('HI');
});

var app = express();
app.use(express.vhost('example.com', vhostApp));
app.listen(80);

Upvotes: 3

Related Questions