From bd1d81c1b89ca40cce5a802e9f124e090f104865 Mon Sep 17 00:00:00 2001 From: Liyas Thomas Date: Thu, 12 Mar 2020 20:15:49 +0530 Subject: [PATCH] :alien: API --- functions/api.js | 21 +++++++++++++++++++++ netlify.toml | 3 +++ 2 files changed, 24 insertions(+) create mode 100644 functions/api.js create mode 100644 netlify.toml diff --git a/functions/api.js b/functions/api.js new file mode 100644 index 000000000..5e95b8332 --- /dev/null +++ b/functions/api.js @@ -0,0 +1,21 @@ +// Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method +exports.handler = async (event, context) => { + switch (event.httpMethod) { + case "GET": + try { + const name = event.queryStringParameters.name || "World" + return { + statusCode: 200, + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ message: `Hello ${name}` }), + } + } catch (err) { + return { statusCode: 500, body: err.toString() } + } + + default: + return { statusCode: 405, body: "Method Not Allowed" } + } +} diff --git a/netlify.toml b/netlify.toml new file mode 100644 index 000000000..ea43d468c --- /dev/null +++ b/netlify.toml @@ -0,0 +1,3 @@ +[build] + command = "npm run generate" + functions = "functions/"