-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
46 lines (39 loc) · 1.17 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
var express = require("express");
var app = express();
const mongoose = require("mongoose");
var bodyParser = require("body-parser");
var cors = require("cors");
var path = require("path");
require("dotenv").config();
// SOME COMMON MIDDLEWARES
app.use(express.json());
app.use(bodyParser.json());
app.use(cors());
app.use(express.static("./client/build"));
// DATABASE CONNECTION
mongoose.connect(
process.env.MONGODB_URI || process.env.DATABASE,
{ useNewUrlParser: true, useUnifiedTopology: true },
function (error) {
if (error) console.log(error);
// Handle failed connection
else
console.log(
"Database connected. Connection State -> " +
mongoose.connection.readyState
);
}
);
// MAIN ROUTES
app.use("/college/teacher", require("./Routes/teacher"));
// SERVE STATIC ASSETS WHEN WE ARE IN PRODUCTION
if (process.env.NODE_ENV === "production") {
app.use(express.static("./client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
var port = process.env.PORT || 8000;
app.listen(port, (req, res) => {
console.log(`Woah! App is running on port ${port}`);
});