-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
42 lines (37 loc) · 1.05 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
const express = require("express");
const app = express();
const cors = require("cors");
const mongoose = require("mongoose");
const { PORT, mongoUri } = require("./config/config");
const noteRoute = require("./routes/note.routes");
const path = require("path");
const { env } = require("process");
app.use(cors());
app.use(express.urlencoded({ extended: true })); //Parse URL-encoded bodies
app.use(express.json()); //Used to parse JSON bodies
mongoose.Promise = global.Promise;
mongoose
.connect(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(
() => {
console.log("MongoDB database is connected");
},
(err) => {
console.log("Failed to connect to the database" + err);
}
);
app.use("/notes", noteRoute);
// Handle production
if (process.env.NODE_ENV === "production") {
// static folder
app.use(express.static("client/dist"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "dist", "index.html"));
});
}
app.listen(PORT, function () {
console.log("Server is running on Port:", PORT);
});