-
Notifications
You must be signed in to change notification settings - Fork 32
/
app.js
112 lines (86 loc) · 2.69 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
104
105
106
107
108
109
110
111
112
const createError = require("http-errors");
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const morgan = require("morgan");
const winston = require("./winston");
const rateLimit = require("express-rate-limit");
const helmet = require("helmet");
const xss = require("xss-clean");
const hpp = require("hpp");
const cors = require("cors");
const compression = require("compression");
const chalk = require("chalk");
const indexRouter = require("./routes/index");
const usersRouter = require("./routes/users");
const quizRouter = require("./routes/quizRouter");
const config = require("./config");
const mongoose = require("mongoose");
const Quizes = require("./models/quizes");
const DB = config.mongoUrl;
mongoose.set("autoIndex", true);
const connectDB = async () => {
const con = await mongoose.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
autoIndex: true,
});
console.log(
chalk.bgGreen.black(`MongoDB Connected: ${con.connection.host}.`)
);
};
connectDB();
var app = express();
app.enable("trust proxy");
// Set Body parser, reading data from body into req.body
app.use(express.json({ limit: "10kb" }));
app.use(express.urlencoded({ extended: true, limit: "10kb" }));
// Set security HTTP headers
app.use(helmet());
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");
app.use(morgan("combined", { stream: winston.stream }));
app.use(cookieParser("12345-67890"));
// Limit requests from the same API
const limiter = rateLimit({
max: 100,
windowMs: 60 * 60 * 1000,
messege: "Too many requests from this IP, Please try again in an hour!",
});
app.use("/", limiter);
// Data sanitization against XSS
app.use(xss());
// Prevent http param pollution
app.use(hpp());
// Implement CORS
app.use(cors());
app.options("*", cors());
app.use(compression());
app.disable("x-powered-by");
// Set static folder
app.use(express.static(path.join(__dirname, "public")));
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/quizes", quizRouter);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
winston.error(
`${err.status || 500} - ${err.message} - ${req.originalUrl} - ${
req.method
} - ${req.ip}`
);
// render the error page
res.status(err.status || 500);
res.render("error");
});
module.exports = app;