-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (36 loc) · 1.16 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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
require("dotenv").config();
const cors = require("cors");
const app = express();
app.use(cors());
const MONGO_CONNECTION_STRING = process.env.MONGODB_CONNECTION_STRING;
const MONGODB_URI = `${process.env.MONGODB_URL}/${process.env.DB_NAME}`;
//import your models
require("./models/quote");
require("./models/Product");
mongoose
.connect(MONGO_CONNECTION_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("MongoDB has been connected"))
.catch((err) => console.log(err));
//middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//import routes
require("./routes/route.js")(app);
const PORT = process.env.PORT || 5000;
// Accessing the path module
const path = require("path");
// Step 1:
app.use(express.static(path.resolve(__dirname, "./client/build")));
// Step 2:
app.get("*", function (request, response) {
response.sendFile(path.resolve(__dirname, "./client/build", "index.html"));
});
app.listen(PORT, () => {
console.log(`server running on port ${PORT}`);
});