-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (47 loc) · 1.53 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
const express = require('express');
const rateLimit = require('express-rate-limit');
const path = require('path');
const quotes = require('./quotes');
const app = express();
const PORT = process.env.PORT || 3000;
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 200, // Limit each IP to 100 requests per windowMs
handler: (req, res) => {
res.status(429).json({
message: 'Too many requests from this IP, please try again after 15 minutes'
});
}
});
app.use(limiter);
app.use(express.static(path.join(__dirname, 'public')));
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'FreeQuotesAPI.html'));
});
app.get("/docs", (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'Docs.html'));
});
app.get("/api/quotes", (req, res) => {
res.status(200).json(quotes);
})
app.get("/api/random", (req, res) => {
var randomIndex = Math.floor(Math.random() * quotes.length);
var randomQuote = quotes[randomIndex];
res.status(200).json(randomQuote);
});
app.get('/api/quotes/:id', (req, res) => {
const id = parseInt(req.params.id);
const quote = quotes.find(q => q.id === id);
if (quote) {
res.status(200).json(quote);
} else {
res.status(404).send({ message: 'Quote not found please try again with different id :-(' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});