Skip to content

Commit 3f2abd0

Browse files
committedJan 17, 2025
Production ready
1 parent dbf3b48 commit 3f2abd0

File tree

6 files changed

+17
-5
lines changed

6 files changed

+17
-5
lines changed
 

‎backend/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "1.0.0",
44
"main": "src/index.js",
55
"scripts": {
6-
"dev": "nodemon src/index.js"
6+
"dev": "nodemon src/index.js",
7+
"start": "node src/index.js"
78
},
89
"keywords": [],
910
"author": "",

‎backend/src/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import dotenv from "dotenv";
33
import cookieParser from "cookie-parser";
44
import cors from "cors";
55

6+
import path from "path";
7+
68
import authRoutes from "./routes/auth.route.js";
79
import messageRoutes from "./routes/message.route.js";
810
import { connectDB } from "./lib/db.js";
@@ -11,6 +13,7 @@ import { app, server } from "./lib/socket.js";
1113
dotenv.config();
1214

1315
const PORT = process.env.PORT
16+
const __dirname = path.resolve();
1417

1518
app.use(express.json());
1619
app.use(cookieParser());
@@ -22,6 +25,13 @@ app.use(cors({
2225
app.use("/api/auth", authRoutes);
2326
app.use("/api/messages", messageRoutes);
2427

28+
if(process.env.NODE_ENV === "production"){
29+
app. use(express.static(path.join(__dirname, "../frontend/dist")));
30+
app.get('*', (req, res) => {
31+
res.sendFile(path.join(__dirname, "../frontend", "dist", "index.html"));
32+
})
33+
}
34+
2535
server.listen(PORT, ()=>{
2636
console.log(`Server running on port: ${PORT}`);
2737
connectDB()

‎frontend/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<title>Vite + React</title>
7+
<title>iChat</title>
88
</head>
99
<body>
1010
<div id="root"></div>

‎frontend/src/lib/axios.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from "axios"
22

33
export const axiosInstance = axios.create({
4-
baseURL: "http://localhost:5001/api",
4+
baseURL: import.meta.env.MODE === "development" ? "http://localhost:5001/api" : "/api",
55
withCredentials: true
66
})

‎frontend/src/store/useAuthStore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { axiosInstance } from "../lib/axios.js"
33
import toast from "react-hot-toast";
44
import { io } from "socket.io-client";
55

6-
const BASE_URL = "http://localhost:5001";
6+
const BASE_URL = import.meta.env.MODE === "development" ? "http://localhost:5001" : "/";
77

88
export const useAuthStore = create((set, get) => ({
99
authUser: null,

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "1.0.0",
44
"main": "index.js",
55
"scripts": {
6-
"test": "echo \"Error: no test specified\" && exit 1"
6+
"build": "npm install --prefix backend && npm install --prefix frontend && npm run build --prefix frontend",
7+
"start": "npm run start --prefix backend"
78
},
89
"repository": {
910
"type": "git",

0 commit comments

Comments
 (0)
Please sign in to comment.