-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
84 lines (69 loc) · 2.09 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
let express = require("express");
let mongoose = require("mongoose");
let cors = require("cors");
let bodyParser = require("body-parser");
let dbConfig = require("./database/db");
let path = require("path");
// To externalize or put data out of source code into a .env file. Didn't use CRA so I don't have it
require("dotenv").config();
const studentR = require("./routes/student.routes");
//let pa = require("./../../kumon_schedule/src/index.html");
// Mongo connection
mongoose.Promise = global.Promise;
// Getting variable
//let dbConfig = process.env.MONGODB_URL;
const app = express();
// Getting Cors to connect
app.all("/*", function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
res.header("Access-Control-Allow-Methods", "GET, POST", "PUT");
next();
});
// Connect the database to schema
mongoose
.connect(process.env.MONGODB_URL || dbConfig.db, {
useNewUrlParser: true
})
.then(
() => {
console.log("Database sucessfully connected!");
},
error => {
console.log("Could not connect to database : " + error);
}
);
// Loading up all of the front end files to serve
app.use(express.static(path.join(__dirname, "Client", "dist")));
app.use(cors());
// Parse the data in easily readable format
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
// Might need to change this
app.use("/students", studentR);
app.get("/students", (req, res) => {
res.sendStatus(212);
//res.sendFile(path.join(__dirname, "Client", "dist", "index.html"));
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "Client", "dist", "index.html"));
});
// PORT
const port = process.env.PORT || 4000;
const server = app.listen(port, () => {
console.log("Connected to port " + port);
});
// 404 Error
app.use((req, res, next) => {
next(createError(404));
});
// Unkown error
app.use(function(err, req, res, next) {
console.error(err.message);
if (!err.statusCode) err.statusCode = 500;
res.status(err.statusCode).send(err.message);
});