Search code examples
node.jsexpresshttpshostname

https.listen ignores hostname


I tried to bind the https server to my subdomain (cdn.somedomain.com). But https.listen(443, 'cdn.somedomain.com') ignores the hostname. He trys to bind the ip and thus all adresses.

var fs = require('fs');
var https = require('https');
var express = require('express');
var app = express();
var subdomain = require('express-subdomain');
var router = express.Router();
var options = {
   key  : fs.readFileSync('/path/to/privkey.pem'),
   cert : fs.readFileSync('/path/to/cert.pem'),
   hostname: 'cdn.somedomain.com'
};

router.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
});
router.use(express.static('somefiles'));
app.use(subdomain('cdn', router));

https.createServer(options, app).listen(443, 'cdn.somedomain.com');

I already tried to use 'express-subdomain', how you can see in my code.

I hope you can help me.

Nils


Solution

  • Sounds like you want virtual hosting, where your server will only pass requests matching a particular hostname (cdn.somedomain.com) to the router.

    You can use the vhost module for that:

    var vhost = require('vhost');
    ...
    app.use(vhost('cdn.somedomain.com', router));