-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (80 loc) · 2.38 KB
/
index.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
// IMPORT EXPRESS MODULE
const express = require("express");
require("express-async-errors");
const app = express();
// IMPORT MONGOOSE MODULE
const mongoose = require("mongoose");
// IMPORT BODY PARSER MODULE
const bodyParser = require("body-parser");
// IMPORT HELMET MODULE
const helmet = require("helmet");
// IMPORT CORS
const cors = require("cors");
// IMPORT RATE LIMIT MODULE
const rateLimit = require("express-rate-limit");
// RATE LIMITS
const limiter = rateLimit({
windowMs: 3 * 60 * 1000,
max: 1000,
standardHeaders: true,
legacyHeaders: false,
});
// STORE CONFIG IN ENV VAR
process.env["NODE_CONFIG_DIR"] = __dirname + "/config/";
const config = require("config");
// GET DATA FROM CONFIG
const serverPort = config.get("port");
// MIDDLEWARE
// const error = require("./middleware/error");
// USE MIDDLEWARE
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(limiter);
// ROUTES
const buffy = require("./routes/buffy");
const angel = require("./routes/angel");
const actor = require("./routes/actors");
const director = require("./routes/directors");
const writer = require("./routes/writers");
// USE ERROR MIDDLEWARE
// app.use(error);
// CONNECT TO MONGODB DATABASE
mongoose
.connect(
`mongodb+srv://${process.env.MONGO_USERNAME}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_CLUSTER}.dgzgulx.mongodb.net/?retryWrites=true&w=majority`,
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => console.log("Connection to the database has been successful"))
.catch((err) =>
console.log(
"An error has occurred when trying to connect to the database: ",
err
)
);
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error: "));
db.once("open", function () {
console.log("Connected successfully");
});
// USE PUBLIC FOLDER
app.use(express.static(__dirname + "/public"));
// ROUTES
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.use("/api/buffy", buffy);
app.use("/api/angel", angel);
app.use("/api/actors", actor);
app.use("/api/directors", director);
app.use("/api/writers", writer);
// ESTABLISH API PORT
const port = process.env.PORT || serverPort;
app.listen(port, () => {
console.log(`This API application is currently running on Port ${port}`);
});