-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
108 lines (96 loc) · 2.97 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require("dotenv").config(); // Load environment variables from .env file
const express = require("express");
const villas = require("./routes/villas");
const forms = require("./routes/forms");
const auth = require("./routes/userRoute");
const cors = require("cors");
const errorMiddleware = require("./middleware/errors");
const cookieParser = require("cookie-parser");
const bodyparser = require("body-parser");
const cloudinary = require("cloudinary");
const fileUpload = require("express-fileupload");
const connectDatabase = require("./config/database");
const { connect } = require("./routes/villas");
const path = require("path");
const app = express();
app.use(express.json());
app.use(bodyparser.urlencoded({ extended: true }));
app.use(cors());
app.use(cookieParser());
app.use(
fileUpload({
useTempFiles: true,
})
);
//setting up cloudinary config
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
// Setting up config file
require("dotenv").config({ path: ".env" });
// Handle uncaught exception
process.on("uncaughtException", (err) => {
if (process.env.NODE_ENV === "DEVELOPMENT") {
console.log(`Error: ${err.stack}`);
console.log("Shuttding down the server due to Uncaught Exception");
process.exit(1);
}
if (process.env.NODE_ENV === "PRODUCTION") {
console.log(`Error: ${err.message}`);
console.log("Shuttding down the server due to Uncaught Exception");
process.exit(1);
}
});
//connect to our database
connectDatabase();
//Routes
app.use("/api/v1", auth);
app.use("/api/v1", villas);
app.use("/api/v1", forms);
app.use("/api", require("./routes/upload"));
if (process.env.NODE_ENV === "PRODUCTION") {
//Set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
if (process.env.NODE_ENV === "DEVELOPMENT") {
//Set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
//Error Middleware to handle errors
app.use(errorMiddleware);
const PORT = process.env.PORT || 4000; // set our port
const MODE = process.env.NODE_ENV;
const server = app.listen(PORT, () => {
console.log(
"Sever is connected to port",
PORT,
"and is running in",
MODE,
"mode"
);
});
//Handle unwanted promise rejections
process.on("unhandledRejection", (err) => {
if (process.env.NODE_ENV === "DEVELOPMENT") {
console.log(`Error: ${err.stack}`);
console.log("Shuttding down the server due to Unhandled Promise Rejection");
server.close(() => {
process.exit(1);
});
}
if (process.env.NODE_ENV === "PRODUCTION") {
console.log(`Error: ${err.message}`);
console.log("Shuttding down the server due to Unhandled Promise Rejection");
server.close(() => {
process.exit(1);
});
}
});