-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
67 lines (52 loc) · 1.48 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
"use strict";
// IMPORT EXPRESS MODULE
const express = require("express");
const app = express();
// IMPORT MODULES
const helmet = require("helmet");
const cors = require("cors");
const rateLimit = require("express-rate-limit");
// SET RATE LIMIT
const limit = rateLimit({
windowMs: 3 * 60,
max: 1000,
standardHeaders: true,
legacyHeaders: false,
});
// GET RANDOM FUNCTION FROM ./QUOTESCONTROLLER
const { getRandom, searchQuotes } = require("./quotesController");
// IMPORT QUOTES ARRAY
const quotes = require('./quotes');
// IMPLEMENT MIDDLEWARE
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(limit);
// fs.mkdirSync(logs, { recursive: true });
// USE PUBLIC FOLDER
app.use(express.static(__dirname + "/public"));
app.all("*", function (req, res, next) {
res.set("Access-Control-Allow-Origin", "*");
next();
});
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.get("/v1/quotes/:num?", function (req, res, next) {
res.send(getRandom(req.params.num || 1));
});
app.get("/v1/quotes/search/:term", function (req, res,) {
res.send(searchQuotes(quotes, req.params.term));
})
// USE ERROR MIDDLEWARE
// app.use(error)
// STORE CONFIG IN ENV VAR
process.env["NODE_CONFIG_DIR"] = __dirname + "/config/";
const config = require("config");
// ESTABLISH API PORT
const port = process.env.PORT || config.get("port");
app.listen(port, () => {
console.log(
`The South Park Quotes API is successfully running on Port ${port}`
);
});