-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
51 lines (40 loc) · 1.36 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import path from 'path';
import express from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
dotenv.config();
import connectDB from './config/db.js';
import cookieParser from 'cookie-parser';
import { notFound, errorHandler } from './middleware/errorMiddleware.js';
import userRoutes from './routes/userRoutes.js';
import userBalance from './routes/userBalance.js';
import treeRoutes from './routes/treeRoutes.js';
import historyRoutes from './routes/historyRoutes.js';
const port = process.env.PORT || 5000;
connectDB();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors({
origin: ['https://investworld.vercel.app', 'http://localhost:3000'],
optionsSuccessStatus: 200,
}));
app.use(cookieParser());
app.use('/api/users', userRoutes);
app.use('/api/balance', userBalance);
app.use('/api/tree', treeRoutes);
app.use('/api/history', historyRoutes);
if (process.env.NODE_ENV === 'production') {
const __dirname = path.resolve();
app.use(express.static(path.join(__dirname, '/frontend/dist')));
app.get('*', (req, res) =>
res.sendFile(path.resolve(__dirname, 'frontend', 'dist', 'index.html'))
);
} else {
app.get('/', (req, res) => {
res.send('API is running....');
});
}
app.use(notFound);
app.use(errorHandler);
app.listen(port, () => console.log(`Server started on port ${port}`));