-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (56 loc) · 1.25 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
const fetch = require('node-fetch')
const { json, send } = require('micro')
const { router, get, post, options } = require('micro-fork')
const microCors = require('micro-cors')
const cors = microCors({
origin: '*',
allowMethods: [
'POST',
'GET',
'PUT',
'PATCH',
'DELETE',
'OPTIONS'
],
allowHeaders: [
'X-Requested-With',
'Access-Control-Allow-Origin',
'X-HTTP-Method-Override',
'Content-Type',
'Authorization',
'Accept'
]
})
const getData = async (req, res) => {
const { url } = req.query
const response = await fetch(`${url}`)
const json = await response.json()
send(res, 200, json)
}
const sendData = async (req, res) => {
const { url } = req.query
const body = await json(req)
let fields = {
method: 'POST'
}
if (body.data) {
fields.data = JSON.stringify(body.data);
}
const toSend = await fetch(`${url}`, fields)
const response = await toSend.json()
send(res, 200, response)
}
const optData = async (req, res) => {
const { url } = req.query
const response = await fetch(`${url}`)
send(res, 200, {})
}
const notfound = (req, res) => send(res, 404, 'Not found route')
module.exports = cors(
router()(
get('/api', getData),
options('/api', optData),
post('/api', sendData),
get('/*', notfound),
)
)