使用Node.js Express托管多个网站 [英] Host multiple websites using Node.js Express
问题描述
我在配置两个不同域的Node.js应用程序时遇到问题。有两个目录
/ abc / - > express-admin设置(后端) - > admin.abc.com
和
/ xyz / - >快递设置(前端) - > abc.com
我需要 admin.abc.com / strong>指向express-admin设置和 abc.com 来表达设置。我已经安装了 vhost ,并且这两个网站均侦听到80端口。
已添加
app.use(vhost('abc.com',app)); // xyz / app.js file
app.use(vhost('admin.abc.com',app)); // abc / app.js file
我的问题:
-
永远 已安装,每当我启动这两个应用程序,第二个始终停止。我尝试为这两个应用程序使用不同的端口,但仍然有相同的错误。个人地,他们运行没有问题。
-
我认为我的设置对于域转发来说太复杂了。任何更好的建议?可能我有一个主app.js文件,我可以使用它来将域路由到各自的应用程序,而不使用每个应用程序的app.js。
我不知道你是如何使用vhost的。首先使用vhost方法,您只需运行一个快速应用程序。不是两个这是一个例子。
var express = require('express');
var vhost = require('vhost');
/ *
编辑/ etc / hosts:
127.0.0.1 api.mydomain.local
127.0.0.1 admin.mydomain.local
* /
//要求您的第一个应用程序
var app1 = require(./ app1);
//要求您的第二个应用程序
var app2 = require(./ app2);
// redirect.use(function(req,res){
// if(!module.parent)console.log(req.vhost);
// res .redirect('http://example.com:3000/'+ req.vhost [0]);
//});
// Vhost app
var appWithVhost = module.exports = express();
appWithVhost.use(vhost('api.mydomain.local',app1)); //服务第一个app
appWithVhost.use(vhost('admin.mydomain.local',app2)); //服务第二个app
/ * istanbul ignore next * /
if(!module.parent){
appWithVhost.listen(8000);
console.log('Express on port 8000');
}
您只需运行使用永久启用vhost的主要快捷应用程序。 / p>
I am having problems configuring two different Node.js applications with different domains. Have two directories
"/abc/" -> express-admin setup (backend) -> admin.abc.com
and
"/xyz/" -> express setup (frontend) -> abc.com
I need admin.abc.com to point to express-admin setup and abc.com to express setup. I have vhost installed and both the site listens to port 80.
Have added
app.use(vhost('abc.com', app)); // xyz/app.js file
app.use(vhost('admin.abc.com', app)); // abc/app.js file
My problems:
forever is installed, whenever i start both the apps, the second one is always stopped. I tried using different port for both apps but still having the same error. Individually they run without problems.
I think my setup is too complicated for domain forwarding. Any better suggestions? May be I have a master app.js file which I can use to route the domains to their respective apps without using the app.js of each applications.
I am not sure how you are using the vhost. First of all with vhost approach, you need to run only one express app. Not two. Here is an example.
var express = require('express');
var vhost = require('vhost');
/*
edit /etc/hosts:
127.0.0.1 api.mydomain.local
127.0.0.1 admin.mydomain.local
*/
// require your first app here
var app1 = require("./app1");
// require your second app here
var app2 = require("./app2");
// redirect.use(function(req, res){
// if (!module.parent) console.log(req.vhost);
// res.redirect('http://example.com:3000/' + req.vhost[0]);
// });
// Vhost app
var appWithVhost = module.exports = express();
appWithVhost.use(vhost('api.mydomain.local', app1)); // Serves first app
appWithVhost.use(vhost('admin.mydomain.local', app2)); // Serves second app
/* istanbul ignore next */
if (!module.parent) {
appWithVhost.listen(8000);
console.log('Express started on port 8000');
}
You just need to run the main express app with vhost enabled using forever.
这篇关于使用Node.js Express托管多个网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!