-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapp.js
103 lines (80 loc) · 2.26 KB
/
app.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
// ? FOR TESTING AND DEVELOPMENT PURPOSES
if (process.env.NODE_ENV === "test") {
require("dotenv").config({
path: "test.env",
});
} else {
require("dotenv").config({
path: "config.env",
});
}
const path = require("path");
const express = require("express");
const mongoose = require("mongoose");
// 1. Middlewares
const morgan = require("morgan");
const cookieParser = require("cookie-parser");
const xss = require("xss-clean");
const helmet = require("helmet");
const mongoSanitize = require("express-mongo-sanitize");
const compression = require("compression");
const cors = require("cors");
// 2. Importing routers and error handler
const userRouter = require("./routes/userRouter");
const postRouter = require("./routes/postRouter");
const errorHandler = require("./utils/errorHandler");
const app = express();
if (process.env.NODE_ENV === "development") {
process.env.PWD = __dirname;
} else {
process.env.PWD = process.cwd();
}
// 3. Connect to MongoDB
mongoose
.connect(process.env.MONGO_SRV, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => {
console.log("Connected to database!");
})
.catch((err) => {
console.log("Cannot connect to database:", err);
});
// *? 4. ATTACH MIDDLEWARES
// Cors for CORS handling
app.use(cors());
// Morgan for request logging
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
// Proxies with Heroku, ngnix etc
app.set("trust proxy", 1);
// Parse request body
app.use(express.json());
// For static files
// app.use(express.static(path.join(__dirname, "public")));
app.use(express.static("client/build"));
// Parse cookies
app.use(cookieParser());
// Sanitize xss input
app.use(xss());
// Sanitize mongo NoSQL queries
app.use(mongoSanitize());
// General security headers
app.use(helmet());
// Compress request and response
app.use(compression());
// *? 5. MOUNT API ROUTERS
app.use("/api/v1/users", userRouter);
app.use("/api/v1/posts", postRouter);
app.use("/img", express.static(path.join(process.env.PWD, "public", "img")));
// *? 6. FOR SERVING REACT WEBSITE
app.use((req, res) => {
res.sendFile(path.join(process.env.PWD, "client", "build", "index.html"));
});
// *? 6. ERROR HANDLERS
app.use(errorHandler);
module.exports = app;