Reputation: 368
I'm trying to add a subdomain to an existing node app using Express 3.0 and express-subdomain.
(I've added the subdomain to my hosts file and that's working fine.)
The current app has all the routes in a separate routes.js file in the same directory as the main file, and it's called like this:
var routes = require("./routes")
//other stuff
routes.routeList(app);
I've tried a bunch of different ways to use the
app.use(subdomain('test-developer', [router]));
syntax and I can't figure it out.
I've tried
var router = require("./routes.js");
app.use(subdomain('test-developer', router));
and I get an error like "The second parameter must be a function that handles fn(req, res, next) params."
here's some more of the code:
//the developerRoutes.js file
var express = require('express');
exports.developerRouteList = function(app) {
var devRouter = express.Router();
devRouter.get('/', function(req, res) {
res.send('hi!');
});
}
and from the main.js file:
var developerRoutes = require('./developerRoutes');
//...
var app = express();
app.use(subdomain('test-developer', developerRoutes.developerRouteList));
//force https with this:
app.enable('trust proxy');
app.configure(function() {
app.use(function (req, res, next){
var hostname = ( req.headers.host.match(/:/g) ) ? req.headers.host.slice( 0, req.headers.host.indexOf(":") ) : req.headers.host
console.log(hostname)
if ((hostname === 'localhost') || (hostname === 'test-developer.localhost') || req.secure) {
// request was via https, so do no special handling
next();
} else {
// request was via http, so redirect to https
res.redirect('https://' + req.headers.host + req.url);
}
});
app.use(express.bodyParser());
app.use(express.cookieParser('secret'));
app.use(function(req, res, next){
session = require("./routes/includes/session.js");
next();
});
app.use(express.static('./public'));
app.use(app.router);
});
app.engine('ejs', engine);
app.set('views',__dirname + '/views');
app.set('view engine', 'ejs');
multiLess.configure(__dirname + '/static/less/', parentDirectory + 'public/css/',['main.less'],0);
routes.routeList(app);
Any ideas on how to sort this out?
Upvotes: 0
Views: 285
Reputation: 569
author of express-subdomain 👋
I've answered this question in a previous issue - see https://github.com/bmullan91/express-subdomain/issues/4. I should probably add that to the readme as there is still a few folk using v3.
Upvotes: 1
Reputation: 453
Based on your posted code, I would try the following steps:
developerRoutes.js
, exports.developerRouteList = function(app)
should be module.exports = devRouter
, and it should be after var devRouter = express.Router()
. Unless you're putting multiple routers in developerRoutes.js
, you don't need to make a sub-object named developerRouteList
within the module.exports
object. Also, it doesn't look like you're using the app
instance you pass in to developerRoutes.js
so it should be okay to change this.main.js
, now you can try app.use(subdomain('test-developer', developerRoutes));
Upvotes: 1