generated from tripleten-com/se_project_express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (50 loc) · 1.36 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
require("dotenv").config();
const express = require("express");
const helmet = require("helmet");
const cors = require("cors");
const mongoose = require("mongoose");
const { errors } = require("celebrate");
const { limiter } = require("./middlewares/limiter");
const { requestLogger, errorLogger } = require("./middlewares/logger");
const { customConsoleLog } = require("./utils/customConsoleLog");
const app = express();
const { PORT = 3001 } = process.env;
const { ErrorHandler } = require("./utils/errors");
// DB Connection
mongoose.set("strictQuery", false);
mongoose
.connect("mongodb://localhost:27017/wtwr_db")
.then(() => {
customConsoleLog("WTWR Database Successfully Connected", "info");
})
.catch(() => {
customConsoleLog("Error Connecting to Database", "error");
});
// log request to request.log
app.use(requestLogger);
// Routes
const routes = require("./routes/index");
app.use(helmet());
app.use(limiter);
app.use(express.json());
app.use(cors());
app.options("*", cors());
app.get("/crash-test", () => {
setTimeout(() => {
throw new Error("Server will crash now");
}, 0);
});
app.use("/", routes);
app.use(errorLogger);
app.use(errors());
app.use(ErrorHandler);
app.listen(PORT, () => {
customConsoleLog(
`
🚀 Server is running!
🔉 Listening on port 3001
📭 Query at http://localhost:3001
`,
"info"
);
});