-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (97 loc) · 3.48 KB
/
server.js
File metadata and controls
115 lines (97 loc) · 3.48 KB
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
113
114
115
const express = require("express");
const https = require("https");
const chalk = require("chalk");
const fs = require("fs");
const helmet = require("helmet");
const winston = require("winston");
const expresswinston = require("express-winston");
const port = process.env.PORT || 3000;
const pretty = require("express-prettify");
const ratelimit = require("express-rate-limit");
const mongoose = require("mongoose");
const ui = require("swagger-ui-express");
const docs = require("./swagger.json");
const limiter = ratelimit({
windowMs: 15 * 60 * 100,
max: 20
});
require("dotenv").config();
mongoose.connect(process.env.DB, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
user: "eiberham",
pass: "eiberham"
});
require("./models/users");
require("./models/characters");
require("./models/sagas");
require("./models/films");
const auth = require("./routes/auth");
const characters = require("./routes/characters");
const sagas = require("./routes/sagas");
const films = require("./routes/films");
const users = require("./routes/users");
const app = express();
app.use(limiter);
app.use(
expresswinston.logger({
transports: [new winston.transports.Console()],
format: winston.format.combine(
winston.format.colorize(),
winston.format.json()
),
meta: true, // optional: control whether you want to log the meta data about the request (default to true)
msg: "http {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
colorize: false, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red).
ignoreRoute(req, res) {
return false;
} // optional: allows to skip some log messages based on request and/or response
})
);
app.use((req, res, next) => {
res.append("Access-Control-Allow-Origin", req.headers.origin || "*");
res.append("Access-Control-Allow-Credentials", "true");
res.append("Access-Control-Allow-Methods", [
"GET",
"OPTIONS",
"PUT",
"POST",
"PATCH",
"DELETE"
]);
res.append(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, X-Access-Token"
);
if (req.method === "OPTIONS") {
res.status(200).end();
} else {
next();
}
});
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(helmet());
app.use(pretty({ query: "pretty" }));
app.set("json spaces", 4);
app.use("/swagger", ui.serve, ui.setup(docs));
app.get("/", (req, res, next) => {
res.send(`Welcome to the dragon ball api, enjoy!`);
});
app.use("/api/auth", auth);
app.use("/api/characters", characters);
app.use("/api/sagas", sagas);
app.use("/api/films", films);
app.use("/api/users", users);
https
.createServer(
{
key: fs.readFileSync("./certs/key.pem"),
cert: fs.readFileSync("./certs/cert.pem"),
passphrase: process.env.TSL_PASSPHRASE
},
app
)
.listen(port, () => console.log(chalk.blue(`Listening on port ${port}`)));