Skip to content

Instantly share code, notes, and snippets.

@banyudu
Created March 18, 2019 05:08
Show Gist options
  • Save banyudu/bad9939c6f45b652952671bb67e72593 to your computer and use it in GitHub Desktop.
Save banyudu/bad9939c6f45b652952671bb67e72593 to your computer and use it in GitHub Desktop.
Lambda code of cors service
import axios from 'axios'
export const get = async (event: any, context: any, callback: any) => {
const parameters = Object.keys(event.queryStringParameters)
if (parameters.length !== 1) {
callback(null, {
statusCode: 400
})
}
const url = parameters[0]
try {
const result = await axios.get(url)
callback(null, {
statusCode: 200,
headers: {
'content-type': result.headers['content-type'] || 'text/html; charset=utf-8',
'Access-Control-Allow-Origin': 'https://banyudu.com',
'Cache-Control': 'public, max-age=3600'
},
body: result.data
})
} catch (error) {
console.error(error)
throw error
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment