This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (43 loc) · 1.5 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
import 'dotenv/config'
import { URL } from 'url'
import got from 'got'
import micro, { createError } from 'micro'
import { RateLimiterMemory } from 'rate-limiter-flexible'
const PORT = parseInt(process.env.PORT || '3000')
const REQUEST_PER_DURATION = parseInt(process.env.REQUEST_PER_DURATION || '3')
const RATE_LIMIT_DURATION = parseInt(process.env.RATE_LIMIT_DURATION || '1')
const rateLimiter = new RateLimiterMemory({
points: REQUEST_PER_DURATION,
duration: RATE_LIMIT_DURATION
})
const server = micro(async (req, res) => {
const key = req.headers['x-forwarded-for'] || req.socket.remoteAddress
// Avoid multiple request from the same origin
try {
await rateLimiter.consume(key, 1)
} catch (e) {
throw createError(429, 'Rate limit exceeded')
}
// Parse URL parameters
const url = new URL('http://localhost' + req.url)
const endpoint = url.searchParams.get('endpoint')
const dataParam = url.searchParams.get('data')
if (!endpoint || !url) {
const missingParams = []
if (!endpoint) missingParams.push('endpoint')
if (!dataParam) missingParams.push('data')
throw createError(400, `the following params are missing: ${missingParams}`)
}
try {
const { data } = await got
.post(endpoint, {
json: JSON.parse(dataParam)
})
.json()
return data
} catch (e) {
throw createError(500, 'Error running your HTTP-POST request. Ensure that the provided params are valid')
}
})
server.listen(PORT)
console.log(`Server listing on localhost:${PORT}`)