-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
66 lines (55 loc) · 1.85 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
const pagesPath = path.join(__dirname, 'pages');
const corsOptions = {
origin: ['https://ocms-frontend.vercel.app','https://firebasestorage.googleapis.com'],
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 204
};
app.use(cors(corsOptions));
app.use(express.json());
app.use(bodyParser.json());
app.use(express.static(pagesPath));
const classroomRoutes = require('./routes/classroom');
const authRoutes = require('./routes/auth');
app.use('/classes', classroomRoutes);
app.use('/auth', authRoutes);
app.get('/', (req, res) => {
// res.send("Welcome to OCMS Backend for Software Engineering Project ");
res.sendFile(path.join(pagesPath, 'WelcomePage.html'));
})
// if ( process.env.NODE_ENV === "production" || 1) {
// app.use(express.static(path.join(__dirname, "../client/build")));
// app.get("*", (req, res) => {
// res.sendFile(path.resolve(__dirname, '../', 'client', 'build', 'index.html'));
// })
// }
app.use((req, res, next) => {
const err = new Error("Not Found");
err.statusCode = 404;
next(err);
})
app.use((err, req, res, next) => {
console.log(err);
const status = err.statusCode || 500;
const message = err.message;
res.status(status).json({message: message});
})
const DB_URL = process.env.MONGO_DB_URI;
const PORT= process.env.PORT || 5000;
mongoose.connect(DB_URL)
.then(result => {
console.log("Connected to database");
app.listen(5000,()=>console.log("Welcome to OCMS Backend"));
console.log("Server started at port 5000");
console.log("http://localhost:5000");
})
.catch(err => {
console.log(err);
})