简体   繁体   English

"使用 Node.js Express 托管多个网站"

[英]Host multiple websites using Node.js Express

I am having problems configuring two different Node.js applications with different domains.我在配置具有不同域的两个不同 Node.js 应用程序时遇到问题。 Have two directories有两个目录

"/abc/" -> express-admin setup (backend) -> admin.abc.com

I am not sure how you are using the vhost. 我不确定您如何使用虚拟主机。 First of all with vhost approach, you need to run only one express app. 首先,使用虚拟主机方法,您只需要运行一个Express应用程序。 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. 您只需要运行永久启用vhost的主要Express应用即可。

You're hosting the applications on the same port, using the same network interface. 您使用相同的网络接口将应用程序托管在相同的端口上。 So when the second app starts, it will always find the port in use. 因此,当第二个应用程序启动时,它将始终找到正在使用的端口。 If you want to use multiple applications on the same port, they each need to have their own network interface. 如果要在同一端口上使用多个应用程序,则每个应用程序都需要具有自己的网络接口。 When using vhost, you would still need to listen on a different port for each app. 使用虚拟主机时,您仍然需要在每个应用程序的不同端口上进行监听。 See this example for details. 有关详细信息,请参见此示例 If you would like your apps to be completely independent, you're better off using node-http-proxy. 如果您希望自己的应用程序完全独立,那么最​​好使用node-http-proxy。 This allows you to host a proxy on port 80 which forwards requests to express apps listening on different ports. 这使您可以在端口80上托管代理,该代理转发请求以表达在不同端口上侦听的应用程序。 If one of these apps crashes, it will not crash the other app, unlike the vhosts approach. 如果这些应用程序之一崩溃,它将不会使其他应用程序崩溃,这与虚拟主机方法不同。 This post gives an example of the implementation using node-http-proxy. 这篇文章提供了使用node-http-proxy的实现示例。

Thanks @veggiesaurus for pointing up to node-http-proxy. 感谢@veggiesaurus指向node-http-proxy。 Apologies for posting late. 抱歉,发帖时间晚了。

Here is how I solved my problem using node-http-proxy 这是我使用node-http-proxy解决问题的方法

Folder Structure: 资料夹结构:

  • www/ 万维网/
    • server.js server.js
    • abc/ [express setup] abc / [快速设置]
      • app.js app.js
    • xyz/ [express-admin setup] xyz / [express-admin设置]
      • node_modules/express-admin/app.js node_modules / express-admin / app.js

"abc" and "xyz" have there own setup and running on port xxxx:3001 and xxxy:3002 “ abc”和“ xyz”具有自己的设置,并在端口xxxx:3001和xxxy:3002上运行

I installed node-http-proxy and added server.js file with following codes. 我安装了node-http-proxy并使用以下代码添加了server.js文件。 Referred this link 引用了此链接

var http = require('http');
var httpProxy = require('http-proxy');

var proxy = httpProxy.createProxy();
var options = {  
  'abc.com': 'http://x.x.x.x:3001',
  'xyz.com': 'http://x.x.x.y:3002'
}

http.createServer(function(req, res) {
  proxy.web(req, res, {
    target: options[req.headers.host]
  });
}).listen(80);

Finally, used forever to run all 3 apps setup to run forever in port 3001, 3002 and 80. 最后,永久用于运行所有3个应用程序的设置,使其永久在端口3001、3002和80中运行。

const express = require("express");
const app = express();
const fs = require('fs');
app.use((req, res, next) => {
    let reqDomain = req.get("host");
    if (reqDomain.indexOf(":") > -1) {
        reqDomain = reqDomain.split(":")[0];
    }
    if(reqDomain.endsWith(".local")) {
        reqDomain = reqDomain.substring(0, reqDomain.length - 6);
    }
    const domainPath = "public/" + reqDomain;
    let filePath = domainPath + req.originalUrl;
    filePath = fs.lstatSync(filePath).isDirectory() ? filePath + "/index.html" : filePath;
    console.log(__dirname + "/" + filePath);
    res.sendFile(filePath, { root: __dirname });
});
const port = process.env.PORT || 80;
app.listen(port, () => console.log("Server Started on Port " + port));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM