Reputation: 455
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
Upvotes: 0
Views: 373
Reputation: 203419
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));
Upvotes: 1
Reputation: 48
The listen function is taking your hostname parameter and resolving it to an ip address, hence why it "ignores" the hostname.
source: node.js:: what does hostname do in `listen` function?
Don't you want it to bind all addresses? Or are you trying to ignore your main domain?
Use your routers to handle the different domains:
var v1Routes = express.Router();
var v2Routes = express.Router();
v1Routes.get('/', function(req, res) {
res.send('API - version 1');
});
v2Routes.get('/', function(req, res) {
res.send('API - version 2');
});
router.use(subdomain('*.v1', v1Routes));
router.use(subdomain('*.v2', v2Routes));
This is right from the documentation: https://github.com/bmullan91/express-subdomain
Upvotes: 1