-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
64 lines (52 loc) · 1.58 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
60
61
62
63
64
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const path = require("path");
//routes connecting to job route
const jobs = require("./routes/api/jobs");
//route connecting to the user route
const users = require("./routes/api/user");
//route connecting to the contact route
// const contact = require("./routes/api/contact");
// Adding json checking middleware
const withAuth = require("./middleware/middleware");
//db
const db = require("./config").mongoURI;
//using cors
app.use(cors());
//BodyParser Middelware
app.use(bodyParser.json());
//using cookie Parser
app.use(cookieParser());
//Additional code
/*Adds the react production build to serve react requests*/
app.use(express.static(path.join(__dirname, "./client/build")));
/*React root*/
//connecting to moongo db
mongoose
.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
.then(() => console.log("moongose connected"))
.catch(err => console.log(err));
//Use Routes
app.get("/checkToken", withAuth, function(req, res) {
res.sendStatus(200);
});
app.use("/api/jobs", jobs);
app.use("/api/users/", users);
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
// app.use("/api/contact", contact);
//assigning port the server
const port = process.env.PORT || 5000;
//listening to the server
app.listen(port, function() {
console.log("green server started on " + port);
});