-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (47 loc) · 1.5 KB
/
server.js
File metadata and controls
58 lines (47 loc) · 1.5 KB
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
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const path = require("path");
// To prevent Access-Control-Allow-Origin errors
const cors = require("cors");
const allowCrossDomain = (req, res, next) => {
res.header("Access-Control-Allow-Origin", "http://localhost:3000");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
res.header("Access-Control-Allow-Headers", "*");
if (req.method === "OPTIONS") {
res.send(200);
} else {
next();
}
};
const app = express();
//Bodyparser Middleware
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: false,
})
);
// DB Config
const db = require("./config/keys").mongoURI;
// Connect to Mongo
mongoose
.connect(db, { useNewUrlParser: true })
.then(() => console.log("MongoDB successfully connected"))
.catch((err) => console.log(err));
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server up and running on port ${port} !`));
//Allow Cross Domain to prevent CORS errors
app.use(cors());
app.use(allowCrossDomain);
//routes middleware
app.use("/api/demo", require("./routes/demo"));
app.use("/api/buyer", require("./routes/buyer"));
app.use("/api/signup", require("./routes/signup"));
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"));
});
}