I want to create a subdomain in the meanjs app.
main- example.com
subdomain1- team.example.com
subdomain2- dashboard.example.com
Where team and dashboards are modules in meanjs.
How do I do this?
I want to create a subdomain in the meanjs app.
main- example.com
subdomain1- team.example.com
subdomain2- dashboard.example.com
Where team and dashboards are modules in meanjs.
How do I do this?
Here's an example for a MEAN stack using subdomains. All of them run separate Express applications. One for the Angular application which serves a static directory which always redirects to index.html so that HTML5mode is possible, one for the backend API that uses Mongoose with MongoDB which serves the content for Angular application. The other two subdomains serve static library assets and images. Together it works as one application:
var express = require('express'),
vhost = require('vhost'),
path = require('path'),
stack = express();
stack.use(vhost('lib.example.org', require(path.join(__dirname, 'lib/index'))));
stack.use(vhost('img.example.org', require(path.join(__dirname, 'img/index'))));
stack.use(vhost('app.example.org', require(path.join(__dirname, 'app/index'))));
stack.use(vhost('api.example.org', require(path.join(__dirname, 'api/index'))));
stack.listen(80);
Edit because of the comments on Vishal's answer and this answer, a session sharing example.
Consider the following app running on app.example.org
:
var express = require('express'),
mongoose = require('mongoose'),
session = require('express-session'),
cookie = require('cookie-parser'),
store = require('connect-mongo')(session),
app = module.exports = express();
mongoose.connect(options);
app.use(cookie());
app.use(session({
name: 'global',
secret: 'yoursecret',
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
resave: true,
saveUninitialized: true,
rolling: true,
cookie: {
path: '/',
domain: 'example.org',
maxAge: null,
httpOnly: false
}
}));
app.use('/', function (req, res) {
req.session.appValue = 'foobar';
res.status(200).end();
});
And the following app running on api.example.org
:
var express = require('express'),
mongoose = require('mongoose'),
session = require('express-session'),
cookie = require('cookie-parser'),
store = require('connect-mongo')(session),
api = module.exports = express();
mongoose.connect(options);
api.use(cookie());
api.use(session({
name: 'global',
secret: 'yoursecret',
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
resave: true,
saveUninitialized: true,
rolling: true,
cookie: {
path: '/',
domain: 'example.org',
maxAge: null,
httpOnly: false
}
}));
api.use('/', function (req, res) {
res.send(req.session.appValue).end();
});
Now when you first visit app.example.org
the session value is set and when afterwards visiting api.example.org
the value is retreived and sent as response. Simple as that. Also take a look at this question and answer: Using Express and Node, how to maintain a Session across subdomains/hostheaders The key here is to set the cookie's domain to example.org
so that it can be accessed on all subdomains. Ofcourse your session store needs to be available to them too.
So you can easily do a teams
and dashboard
subdomain, login once and be logged in on both subdomains at the same time. Hope that clears things up. Remember everything is possible and there is no "correct" way of designing your structure. It's all about how you want to separate your concerns, it's you (and your team) who's got to work with it, not somebody else.
Sub domains are meant for creating separate websites. You can configure multiple mean applications under each sub domain. They can be completely independent. In fact one can be a mean application. Another can be a wordpress site.
The configuration for different sub domains cannot be done from inside your web application.
Here is one way to host separate mean applications from the same server. Have a wildcard entry for the domain. Then use Nginx proxy for the sub domain mapping.