-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (38 loc) · 1.12 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
const express = require('express');
const rateLimit = require("express-rate-limit");
const cors = require('cors')
const {
randomQuote,
allQuotes,
randomQuotes,
numOfQuotes
} = require('./handlers');
const app = express();
const limiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: "Too many requests in too small time. Chill. Take a walk or something idk."
});
app.enable("trust proxy");
app.set('view engine', 'ejs');
app.set('json spaces', 4);
app.use(limiter);
app.use(cors());
app.get('/', (req, res) => {
res.render('index', { amount: numOfQuotes() })
})
app.get('/random', (req, res) => {
res.json(randomQuote())
})
app.get('/random/:amount', (req, res) => {
res.json(randomQuotes(req.params.amount))
})
app.get('/random/?number:amount', (req, res) => {
res.json(randomQuotes(req.params.amount))
})
app.get('/all', (req, res) => {
res.json(allQuotes())
})
// Preparing for Heroku
port = process.env.PORT || 3000
app.listen(port, () => console.log(`Server listening on port ${port}!`));