简体   繁体   English

在同一台服务器上运行多个 Node.js 应用程序

[英]Run multiple Node.js apps on the same server

I want to run multiple Node js apps on the same server, and so far I made some progress checking solutions for similar questions here (links below).我想在同一台服务器上运行多个 Node js 应用程序,到目前为止,我为这里的类似问题做了一些进度检查解决方案(下面的链接)。 Let's say I have 2 apps, each serving some html file, and I'd like to access each by visiting https://example.com/app1 and https://example.com/app2假设我有 2 个应用程序,每个应用程序都提供一些 html 文件,我想通过访问https://example.com/app1https://example.com/app2来访问每个应用程序
So far, I have my main app, and my approach was to call this app which will then redirect a client to one of these 2 apps.到目前为止,我有我的主应用程序,我的方法是调用这个应用程序,然后将客户端重定向到这两个应用程序之一。
My main app looks like this:我的主应用程序如下所示:

const express = require('express');
const app = express();

app
  .use('/app1', require('./app1/index.js'))
  .use('/app2', require('./app2/index.js'))
  .listen(80);

Each of my two sub-apps (app1 and app2) looks like this我的两个子应用程序(app1 和 app2)中的每一个看起来像这样

const express = require('express');
const bodyParser = require('body-parser');
const routes = require('./routes/api');
const mongoose = require('mongoose');
require('dotenv/config');

const app = express();

mongoose.connect(
    process.env.DB_CONNECTION,
    { useNewUrlParser: true, useUnifiedTopology: true }, () =>
    console.log('Connected to DB')
);
mongoose.Promise = global.Promise;

app.use(express.static('public'));
app.use(bodyParser.json());

app.use('/', routes); 

app.use(function (err, req, res, next) {
    res.status(422).send({ error: err.message })
});

The issue is that I don't get anything after deploying these apps and visiting eg https://example.com/app1问题是在部署这些应用程序并访问例如https://example.com/app1后我什么也没得到
I'm super new in all this so there is likely a beginner's mistake in here.我对这一切都非常陌生,所以这里可能是初学者的错误。 Can anyone help?任何人都可以帮忙吗?

Related questions How to mount express.js sub-apps?相关问题如何挂载 express.js 子应用? and Running multiple Node (Express) apps on same port在同一端口上运行多个 Node (Express) 应用程序

If you want to run totally different application in node you might use proxy_pass / reverse proxy of apache/nginx.如果您想在节点中运行完全不同的应用程序,您可以使用 apache/nginx 的proxy_pass / reverse proxy To do so each of your app should operate on theirs own ports and some other server (apache/nginx/etc) passing requests to each of them为此,您的每个应用程序都应该在自己的端口和其他一些服务器(apache/nginx/etc)上运行,将请求传递给每个应用程序

Im hosting several node apps using this technique and they are working really nice (nginx is much faster than apache).我使用这种技术托管了几个节点应用程序,它们运行得非常好(nginx 比 apache 快得多)。 Also you might thinking about blocking access from internet to node apps ports directly.此外,您可能会考虑直接阻止从 Internet 访问节点应用程序端口。

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

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