From 393e9c2652fe080f55058ec018dce8c30b660d34 Mon Sep 17 00:00:00 2001 From: Badri Sunderarajan Date: Fri, 4 Jun 2021 23:46:11 +0530 Subject: [PATCH] Allow changing of root subdirectory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So that the app can be deployed anywhere! Just change the environment variable `CHIPCHOC_BASE_URL` and you're all set 😉 --- .sample-env | 1 + README.md | 1 + server.js | 13 ++++++++++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.sample-env b/.sample-env index 2d11661..ca64ffa 100644 --- a/.sample-env +++ b/.sample-env @@ -3,3 +3,4 @@ CHIPCHOC_EMAIL_PORT=587 CHIPCHOC_EMAIL_SECURE=false CHIPCHOC_EMAIL_USER=user@example.com CHIPCHOC_EMAIL_PASSWORD=badexample +CHIPCHOC_BASE_URL=/ diff --git a/README.md b/README.md index 6a84093..3154eed 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ CHIPCHOC_EMAIL_PORT=587 CHIPCHOC_EMAIL_SECURE=false CHIPCHOC_EMAIL_USER=user@example.com CHIPCHOC_EMAIL_PASSWORD=badexample +CHIPCHOC_BASE_URL=/ ``` ...then start [Rollup](https://rollupjs.org): diff --git a/server.js b/server.js index a5addd6..c20a4ac 100644 --- a/server.js +++ b/server.js @@ -3,6 +3,7 @@ const nodemailer = require('nodemailer') require('dotenv').config() +// handle debug mode let DEBUG if (process.env.DEBUG || process.env.CHIPCHOC_DEBUG) { @@ -25,9 +26,15 @@ const perumal = nodemailer.createTransport({ }, }) +// decide base url for app (can be configured) +let baseUrl = process.env.CHIPCHOC_BASE_URL || '/' + const app = express() var expressWs = require('express-ws')(app) +const router = express.Router() +app.use(baseUrl, router) + const port = process.env.CHIPCHOC_PORT || 5000 const cors = require('cors') @@ -35,7 +42,7 @@ const path = require('path') app.use(cors()) -app.get('/air', (req, res) => { +router.get('/air', (req, res) => { console.log(`Searching for contacts with the term: ${req.query.name}`) res.send({ success: true, @@ -44,7 +51,7 @@ app.get('/air', (req, res) => { }) -app.ws('/hit-send', (ws, request) => { +router.ws('/hit-send', (ws, request) => { console.log('Hitting send on some emails!') ws.on('message', async (message) => { @@ -92,7 +99,7 @@ app.ws('/hit-send', (ws, request) => { }) app.use(express.static('public')) -app.get('/', (req, res) => { +router.get('/', (req, res) => { res.sendFile(path.resolve(__dirname, 'public', 'index.html')) })