-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
40 lines (34 loc) · 1.21 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
const express = require('express')
const app = express()
const rateLimit = require("express-rate-limit")
const PORT = process.env.PORT || 8000
const JSONdb = require('simple-json-db');
const db = new JSONdb(`${__dirname}/database.json`)
// Rate limiter
const limiter = rateLimit({
windowMs: 100 * 60 * 100, // 10 minutes
max: 100, // 100 requests max every 10 minutes
message: "You are sending too many requests. Please try again later."
})
//API Requests
app.get('/*', (req, res, next) => {
db.set('requests', (db.get('requests') + 1))
console.log(`${req.headers['x-forwarded-for']} ${req.connection.remoteAddress} ${req.url}`)
next()
})
//Endpoints
app.get('/hello', (req, res) => {
res.status(200).send({
message: "Hello World!"
});
});
app.get('/cats', (req, res) => {
res.send({
jpg: "https://cdn.universal-network.xyz/cats/" + 'cat%20' + '(' + Math.floor(Math.random() * 153) + ')' + ".jpg", jpeg: "https://cdn.universal-network.xyz/cats/" + 'cat%20' + '(' + Math.floor(Math.random() * 37) + ')' + ".jpeg"
});
});
// Listen on port
app.listen(PORT, () => console.log(`Server started on port ${PORT}`))
app.listen(3000, () => {
console.log('API Online');
});