Created
October 23, 2024 06:28
-
-
Save Akshay090/3f556028f2d5d566b0c424f9d3795e7e to your computer and use it in GitHub Desktop.
Basic CORS Proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import https from 'https'; | |
| export const handler = async (event) => { | |
| const apiUrl = event.queryStringParameters?.url; | |
| if (!apiUrl) { | |
| return { | |
| statusCode: 400, | |
| headers: { | |
| 'Access-Control-Allow-Origin': '*', | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ message: 'Missing query parameter: url' }), | |
| }; | |
| } | |
| try { | |
| const data = await makeApiCall(apiUrl); | |
| return { | |
| statusCode: 200, | |
| headers: { | |
| 'Access-Control-Allow-Origin': '*', | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify(data), | |
| }; | |
| } catch (error) { | |
| console.error('Error fetching data:', error); | |
| return { | |
| statusCode: 500, | |
| headers: { | |
| 'Access-Control-Allow-Origin': '*', | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ message: 'Internal Server Error' }), | |
| }; | |
| } | |
| }; | |
| const makeApiCall = (url) => { | |
| return new Promise((resolve, reject) => { | |
| https.get(url, (res) => { | |
| let body = ''; | |
| res.on('data', (chunk) => { | |
| body += chunk; | |
| }); | |
| res.on('end', () => { | |
| resolve(JSON.parse(body)); | |
| }); | |
| }).on('error', (e) => { | |
| reject(e); | |
| }); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment