Allow changing of root subdirectory

So that the app can be deployed anywhere! Just change the
environment variable `CHIPCHOC_BASE_URL` and you're all set 😉
This commit is contained in:
Badri Sunderarajan 2021-06-04 23:46:11 +05:30
parent 74ecea0af9
commit 393e9c2652
3 changed files with 12 additions and 3 deletions

View file

@ -3,3 +3,4 @@ CHIPCHOC_EMAIL_PORT=587
CHIPCHOC_EMAIL_SECURE=false CHIPCHOC_EMAIL_SECURE=false
CHIPCHOC_EMAIL_USER=user@example.com CHIPCHOC_EMAIL_USER=user@example.com
CHIPCHOC_EMAIL_PASSWORD=badexample CHIPCHOC_EMAIL_PASSWORD=badexample
CHIPCHOC_BASE_URL=/

View file

@ -23,6 +23,7 @@ CHIPCHOC_EMAIL_PORT=587
CHIPCHOC_EMAIL_SECURE=false CHIPCHOC_EMAIL_SECURE=false
CHIPCHOC_EMAIL_USER=user@example.com CHIPCHOC_EMAIL_USER=user@example.com
CHIPCHOC_EMAIL_PASSWORD=badexample CHIPCHOC_EMAIL_PASSWORD=badexample
CHIPCHOC_BASE_URL=/
``` ```
...then start [Rollup](https://rollupjs.org): ...then start [Rollup](https://rollupjs.org):

View file

@ -3,6 +3,7 @@ const nodemailer = require('nodemailer')
require('dotenv').config() require('dotenv').config()
// handle debug mode
let DEBUG let DEBUG
if (process.env.DEBUG || process.env.CHIPCHOC_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() const app = express()
var expressWs = require('express-ws')(app) var expressWs = require('express-ws')(app)
const router = express.Router()
app.use(baseUrl, router)
const port = process.env.CHIPCHOC_PORT || 5000 const port = process.env.CHIPCHOC_PORT || 5000
const cors = require('cors') const cors = require('cors')
@ -35,7 +42,7 @@ const path = require('path')
app.use(cors()) app.use(cors())
app.get('/air', (req, res) => { router.get('/air', (req, res) => {
console.log(`Searching for contacts with the term: ${req.query.name}`) console.log(`Searching for contacts with the term: ${req.query.name}`)
res.send({ res.send({
success: true, 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!') console.log('Hitting send on some emails!')
ws.on('message', async (message) => { ws.on('message', async (message) => {
@ -92,7 +99,7 @@ app.ws('/hit-send', (ws, request) => {
}) })
app.use(express.static('public')) app.use(express.static('public'))
app.get('/', (req, res) => { router.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'public', 'index.html')) res.sendFile(path.resolve(__dirname, 'public', 'index.html'))
}) })