1

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). 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
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

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 e.g. 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? and Running multiple Node (Express) apps on same port

lysf
  • 13
  • 1
  • 1
  • 5
  • you should look into `express.Router` and also export the apps from their separate files, as simply requiring a file won't return anything if there is no export – Krzysztof Krzeszewski Nov 19 '20 at 11:35
  • use a reverse proxy like nginx, you don't need to handle this manually. Run both apps on two different prots. It can match a pattern in url, based on which redirect the request to corresponding app. – Ankit Nov 19 '20 at 11:54
  • I guess this should work properly if you added `module.exports = app` to your app files – Mouneer May 17 '21 at 16:58

1 Answers1

6

If you want to run totally different application in node you might use proxy_pass/reverse proxy of apache/nginx. 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

Im hosting several node apps using this technique and they are working really nice (nginx is much faster than apache). Also you might thinking about blocking access from internet to node apps ports directly.

Seti
  • 2,169
  • 16
  • 26
  • I decided to use nginx in the end, and so far I can see my two apps running when I visit www.mydomain.com:3000 and www.mydomain.com:4000, however, I'd like to open them via e.g. www.mydomain.com/app1. I've changed location in /etc/nginx/sites-available/default to: `location /app1/ { proxy_pass http://myIPaddress:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; }` but I get 'Cannot GET /app1/' on load – lysf Nov 20 '20 at 11:27
  • 1
    Okay, figured it out - I needed just few more lines of code in my reverse proxy, used this as an example https://flaviocopes.com/nginx-reverse-proxy/ – lysf Nov 20 '20 at 13:09
  • glad you made it, was a bit of out of stackoverflow to answer you comment earlier – Seti Nov 21 '20 at 23:07