-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (30 loc) · 1.04 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
const express = require('express');
const cors = require('cors');
const url = require('url');
require('dotenv').config();
const needle = require('needle');
const app = new express();
const PORT = process.env.PORT || 4000;
//Enable CORS
app.use(cors())
// Enviornment variable
const API_URL = process.env.API_URL
const API_KEY_VARIABLE = process.env.API_KEY_VARIABLE
const API_KEY_VALUE = process.env.API_KEY_VALUE
//create the route
app.get("/api", async (req,res) => {
try{
const params = new URLSearchParams({
[API_KEY_VARIABLE] : API_KEY_VALUE,
...url.parse(req.url,true).query //Query parameters passed to the proxy e.g city here
})
//Call the actual api here using needle
const apiResponse = await needle('get',`${API_URL}?${params}`);
const data = apiResponse.body;
res.status(200).json(data);
}
catch(ex) {
res.status(500).json({ex});
}
});
app.listen(PORT , () => console.log(`Proxy server listening at port ${PORT}`));