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

3
  • 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 Commented Nov 19, 2020 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. Commented Nov 19, 2020 at 11:54
  • I guess this should work properly if you added module.exports = app to your app files Commented May 17, 2021 at 16:58

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
Okay, figured it out - I needed just few more lines of code in my reverse proxy, used this as an example flaviocopes.com/nginx-reverse-proxy
glad you made it, was a bit of out of stackoverflow to answer you comment earlier

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.