From 75e4aa3e082dda850cb6fea5ee936efcd41ded70 Mon Sep 17 00:00:00 2001 From: Moon Patel Date: Mon, 13 May 2024 21:41:20 +0530 Subject: [PATCH] PROD mode and backend/.env.example update --- backend/.env.example | 6 +++++- backend/app.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/backend/.env.example b/backend/.env.example index 4ea36c0..a422813 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -15,4 +15,8 @@ PORT=8080 CHESS_ENGINE_PATH=./engine/stockfish16.exe # hostname -HOSTNAME=http://localhost:8080 \ No newline at end of file +HOSTNAME=http://localhost:8080 +# can be PROD or DEV +# DEV - development mode +# PROD - same as DEV but serves frontend statics files from ../frontend/dist using the / endpoint +MODE=DEV \ No newline at end of file diff --git a/backend/app.js b/backend/app.js index e40af8c..2f08dbe 100644 --- a/backend/app.js +++ b/backend/app.js @@ -1,3 +1,5 @@ +const path = require("path"); + const express = require("express"); const bodyParser = require("body-parser"); const cors = require("cors"); @@ -8,6 +10,7 @@ const mongoose = require("mongoose"); const cookieParser = require("cookie-parser"); require("dotenv").config(); +const MODE = process.env.MODE || "DEV" const port = process.env.PORT; mongoose @@ -45,6 +48,18 @@ app.use("/api/auth", authRoutes); app.use("/api/user", userRoutes); app.use("/api/room", roomRoutes); +if (MODE=="PROD") { + console.log('Hello') + app.use(express.static('../frontend/dist')); + app.get("/*", (req,res,next) => { + try { + res.sendFile(path.join(__dirname,"../frontend/dist/index.html")) + } catch(err) { + next(err) + } + }) +} + app.use((error, req, res, next) => { const status = error.status || 500; console.log(error);