-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (48 loc) · 1.25 KB
/
index.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
52
53
54
55
56
57
58
59
import express from 'express';
import dotenv from 'dotenv';
import mongoose from 'mongoose'
import cors from 'cors'
/////////////////
// Routes
/////////////////
import authRoute from './routes/auth.js'
import userDetailsRoute from './routes/userDetails.js'
import consoleRoute from './routes/console.js'
const app = express();
dotenv.config();
//DB Connection
const connectDatabase = async () => {
try {
await mongoose.connect(process.env.DB_PASSWORD);
console.log("connected to database");
} catch (error) {
console.log(error);
process.exit(1);
}
};
connectDatabase();
//Middleware
app.use(express.json());
app.use(cors({ credentials: true, origin: true, exposedHeaders: "*" }));
app.use(function (req, res, next) {
const allowedOrigins = [
"http://localhost:3000",
"https://project-buddy.live",
];
const origin = req.headers.origin;
if (allowedOrigins.includes(origin)) {
res.header("Access-Control-Allow-Origin", origin);
}
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
//Route Middleware
app.use("/user", authRoute);
app.use("/dashboard", userDetailsRoute);
app.use("/console", consoleRoute);
app.listen(5001, () => {
console.log("running");
});