0

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?

Community
  • 1
  • 1
Dipankar
  • 3
  • 1

2 Answers2

0

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.

Community
  • 1
  • 1
iH8
  • 27,722
  • 4
  • 67
  • 76
-1

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.

dreamerkumar
  • 1,540
  • 1
  • 18
  • 28
  • "They can be completely independent.". will i be able to share sessions across? – Dipankar Feb 15 '16 at 00:38
  • i tried nginx today but i dont find it performant enough. all my modules can run on a single app. i just need to map modules to subdomains, if that is possible. – Dipankar Feb 15 '16 at 00:40
  • Completely disagee with @VishalKumar on the sharing sessions between subdomains. What if one needs a api.example.org and app.example.org and you want to access the same session on both subdomains/applications? That should be and is possible. I know loads of implementations like that, they're rather common. Why shouldn't that make sense? – iH8 Feb 17 '16 at 00:47
  • There are ways to set the cookie so that it can be accessed across sub domains. So technically your different sub domains can use that same session id. But they will be separate web applications none the less. If I have to expose an API, I would rather have it as app.example.com/api/... – dreamerkumar Feb 17 '16 at 13:37
  • What you "rather have" doesn't mean that's it's the best for everyone. How people want to separate their concerns is up to them, not to you. There's no such thing as "best practice" when it comes to things like this. He/She and his team need to work with it, not you – iH8 Feb 17 '16 at 13:43