-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
75 lines (59 loc) · 1.9 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
import path, { dirname } from "path";
import dotenv from "dotenv";
dotenv.config();
import express from "express";
const app = express();
import mongoose from "mongoose";
mongoose.set("strictQuery", true);
import "express-async-errors";
import morgan from "morgan";
// Secirity packages
// import cors from "cors";
import helmet from "helmet";
import xss from "xss-clean";
import mongoSanitize from "express-mongo-sanitize";
import cookieParser from "cookie-parser";
// Parsers
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cookieParser());
app.set("trust proxy", 1); //! for rateLimiter and COOKIES, to enable it when behind the reverse proxy (Heroku, Bluemix, etc)
// The rateLimiter is used in the jobRoutes.js
// Importing routes
import authRoutes from "./routes/auth-routes.js";
import jobRoutes from "./routes/job-routes.js";
import notFound from "./middleware/notFound.js";
import errorHandler from "./middleware/errorHandler.js";
import authVerification from "./middleware/auth.js";
import { fileURLToPath } from "url";
const port = process.env.PORT || 9000;
// Activate morgan
if (process.env.NODE_ENV !== "production") {
app.use(morgan("dev"));
}
const __dirname = dirname(fileURLToPath(import.meta.url));
// only when ready to deploy
app.use(express.static(path.resolve(__dirname, "./client/build")));
// Security middleware
app.use(helmet());
app.use(xss());
app.use(mongoSanitize());
// Route middleware
app.use("/api/v1/auth", authRoutes);
app.use("/api/v1/jobs", authVerification, jobRoutes);
app.use(errorHandler);
app.use(notFound);
const start = async () => {
try {
await mongoose
.connect(process.env.MONGODB_URL)
.then(() => console.log("MONGODB connected!"))
.catch((err) => console.log(err));
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
} catch (error) {
console.log(error);
}
};
start();