forked from Gigatorb-Software/MernStackEcommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (37 loc) · 1.27 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
const bodyParser = require("body-parser");
const cors = require("cors");
const dotenv = require("dotenv");
const express = require("express");
const mongoose = require("mongoose");
const path = require("path");
const postRoutes = require("./routes/posts.js");
// Load all environment variables:
dotenv.config();
// Instantiate the application:
const app = express();
// Setup the middlewares:
app.use(bodyParser.json({ limit: "30mb", extended: true }))
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }))
app.use(cors());
// Instruct express to also serve built React client application:
app.use(express.static("./client/build"));
// Routes:
app.use("/posts", postRoutes);
// Configuration:
app.set("port", process.env.PORT || 5000);
const CONNECTION_URL = process.env.DB || "mongodb://localhost:27017/memories";
const PORT = process.env.PORT || 5000;
mongoose.connect(
CONNECTION_URL,
{ useNewUrlParser: true, useUnifiedTopology: true }
).then(() => {
app.listen(PORT, () => {
console.log(`Server Running on Port: http://localhost:${PORT}`);
});
}).catch((error) => {
console.log(`${error} did not connect`);
});
mongoose.set("useFindAndModify", false);
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});