-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (32 loc) · 1.03 KB
/
server.js
File metadata and controls
35 lines (32 loc) · 1.03 KB
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
import express from 'express';
import dotenv from 'dotenv';
import budgetRoute from './routes/budgetRoutes.js';
import userRoute from './routes/userRoute.js';
import errorHandler from './middleware/errorMiddleware.js';
import connectDB from './config/db.js';
import colors from 'colors'; // Allows the use of Colors in terminal.
dotenv.config();
const port = process.env.PORT || 8000;
import cors from 'cors';
connectDB();
const app = express();
const whitelist = ['http://localhost:3000', 'http://localhost:8080'];
const corsOptions = {
origin: function (origin, callback) {
if (!origin || whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
};
app.use(cors(corsOptions));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use('/api/v1/budget', budgetRoute);
app.use('/api/v1/user', userRoute);
app.use(errorHandler);
app.listen(port, () =>
console.log(`Server running on port: ${port}`.underline.yellow)
);