api.js 604 B

123456789101112131415161718192021
  1. // Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method
  2. exports.handler = (event) => {
  3. switch (event.httpMethod) {
  4. case "GET":
  5. try {
  6. const name = event.queryStringParameters.name || "World"
  7. return {
  8. statusCode: 200,
  9. headers: {
  10. "Content-Type": "application/json",
  11. },
  12. body: JSON.stringify({ message: `Hello ${name}` }),
  13. }
  14. } catch (e) {
  15. return { statusCode: 500, body: e.toString() }
  16. }
  17. default:
  18. return { statusCode: 405, body: "Method Not Allowed" }
  19. }
  20. }