How can I configure vhost express to work with three application folders

64 Views Asked by At

I have three folders on a Debian server with three application node (express + ejs + view). Each application is a www site on port 3000. It is clear that this cannot work at the same time. Each individual application - www site works great. I also have an nginx reverse proxy configured that proxies ssl requests (443) to express:3000. I ask for help on how to configure vhost to work with applications in folders. They should all run on port 3000 with its own static and views folders.

Error: Failed to lookup view "index" in views directory "/var/express/vhost/ejs" at Function.render (/var/express/uc.domain.com/dev/node_modules/express/lib/application.js:597:17) at ServerResponse.render (/var/express/uc.domain.com/dev/node_modules/express/lib/response.js:1039:7) at file:///var/express/uc.domain.com/dev/express.js:25:7 at Layer.handle [as handle_request] (/var/express/uc.domain.com/dev/node_modules/express/lib/router/layer.js:95:5) at next (/var/express/uc.domain.com/dev/node_modules/express/lib/router/route.js:144:13) at Route.dispatch (/var/express/uc.domain.com/dev/node_modules/express/lib/router/route.js:114:3) at Layer.handle [as handle_request] (/var/express/uc.domain.com/dev/node_modules/express/lib/router/layer.js:95:5) at /var/express/uc.domain.com/dev/node_modules/express/lib/router/index.js:284:15 at Function.process_params (/var/express/uc.domain.com/dev/node_modules/express/lib/router/index.js:346:12) at next (/var/express/uc.domain.com/dev/node_modules/express/lib/router/index.js:280:10)

it is vhost.js

import express from 'express'
import vhost from 'vhost'
import https from 'https'
import fs from 'fs'
import path from 'path'

import app from '../uc.domain.com/dev/express.js'

const options = {
  key: fs.readFileSync('/var/express/_ssl/helios.uc.domain.com.key'),
  cert: fs.readFileSync('/var/express/_ssl/helios.uc.domain.com.crt'),
  requestCert: false,
  rejectUnauthorized: false,
}

const port = process.env.PORT || 3000
const app = express()

tools.get('/', (req, res) => {
  res.send('This is the tools.uc.domain.com')
})


app.use(vhost('dev.uc.domain.com', app))
app.use(vhost('tools.uc.domain.com', tools))

const server = https.createServer(options, app).listen(port, () => {
  console.log(`- https://dev.uc.domain.com:${port},`)
  console.log(`- https://tools.uc.domain.com:${port}`) 
  console.log('For end of work press Ctrl+C')
})

application-site.js in folder '../uc.domain.com/dev/express.js'

import express from 'express'
import https from 'https'
import fs from 'fs'
import path from 'path'
import router from './system/routes.js'

const options = {
  key: fs.readFileSync('/var/express/_ssl/helios.uc.domain.com.key'),
  cert: fs.readFileSync('/var/express/_ssl/helios.uc.domain.com.crt'),
  requestCert: false,
  rejectUnauthorized: false,
}

const port = process.env.PORT || 3000
const app = express()
app.set('view engine', 'ejs')
app.set('views', path.resolve(process.cwd(), 'ejs'))

app.use(express.static(path.resolve(process.cwd(), 'static')))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(router)

app.get('/', (req, res) => {
  res.render('index', {
    title: 'Sweet home Alabama',
  })
})

app.get('/:sub', (req, res) => {
  res.render(req.params.sub, {
    title: 'Sweet home Alabama',
  })
})

// const server = https.createServer(options, app).listen(port, () => {
//  console.log(`- https://dev.uc.domain.com:${port})
// })

export default app
1

There are 1 best solutions below

0
On BEST ANSWER

I did as @Marc suggested, nginx as a reverse proxy. The more I work in this configuration, the more I like it and now I can manage my applications via pm2. Not sure if this would be possible with vhost

nginx config: /etc/nginx/sites-available/default

server {
    listen 443 ssl;
    server_name tools.uc.domain.com;

    ssl_certificate /var/express/_ssl/helios.uc.domain.com.pem;
    ssl_certificate_key /var/express/_ssl/helios.uc.domain.com.key;

    location / {
        proxy_pass https://localhost:3008;
        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;
    }
}

server {
    listen 443 ssl;
    server_name srv.uc.domain.com;

    ssl_certificate /var/express/_ssl/helios.uc.domain.com.pem;
    ssl_certificate_key /var/express/_ssl/helios.uc.domain.com.key;

    location / {
        proxy_pass https://localhost:3009;
        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;
    }
}