我想在meanjs app中创建一个子域。
main- example.com
subdomain1- team.example.com
subdomain2- dashboard.example.com
团队和仪表板是meanjs中的模块。
我该怎么做?
答案 0 :(得分:0)
这是使用子域的MEAN堆栈的示例。所有这些都运行单独的Express应用程序。一个用于Angular应用程序,它提供一个静态目录,总是重定向到index.html,以便HTML5mode是可能的,一个用于后端API,使用Mongoose和MongoDB,为Angular应用程序提供内容。其他两个子域提供静态库资源和映像。它可以作为一个应用程序一起使用:
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);
编辑,因为对Vishal的答案和这个答案的评论,一个会话共享示例。
考虑在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();
});
以下应用在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();
});
现在,当您第一次访问app.example.org
时,会话值已设置,之后访问api.example.org
时,该值会被检索并作为回复发送。就那么简单。另请查看此问题并回答:Using Express and Node, how to maintain a Session across subdomains/hostheaders此处的关键是将Cookie域设置为example.org
,以便可以在所有子域上访问它。当然,您的会话商店也需要为他们所用。
因此,您可以轻松执行teams
和dashboard
子域,登录一次并同时登录这两个子域。希望这能说明问题。记住一切都是可能的,没有"正确的"设计结构的方式。这完全取决于您如何分离您的疑虑,是您(和您的团队)与谁合作,而不是其他人。
答案 1 :(得分:-1)
子域名用于创建单独的网站。您可以在每个子域下配置多个均值应用程序。他们可以完全独立。实际上,一个可以是一个卑鄙的应用程另一个可以是wordpress网站。
无法从Web应用程序内部完成不同子域的配置。
以下是从同一服务器托管单独的平均应用程序的一种方法。拥有域的通配符条目。然后使用Nginx代理进行子域映射。