Skip to content

Latest commit

 

History

History
130 lines (80 loc) · 4.93 KB

README_step01.md

File metadata and controls

130 lines (80 loc) · 4.93 KB

01. Create a mock REST API with Netlify functions

License Apache2 Discord Actions Status Netlify Status

⚒️ Setup and deploy your first app > 📚 What can Netlify do for you > ⚒️ Create a mock REST API with Netlify functions | next=> 📚 What is DataStax Astra and Stargate

Objectives

+ The REST API is stateless, and therefore helps functions scale horizontally. 

In step 1 of the Battlestax tutorial, we will:

+ Create test cases to check that our API call is working correctly
 
+ Build the API call to Astra to create a game document, based on the requirements from our test

🗓️ Table of Contents

  1. Setup your environment
  2. Making an endpoint using Netlify functions

1. Setup your environment

✅ Step 1a: Checkout expected branch

  1. Switch to branch step-1
  • For this part of the tutorial, we will be working in step-1 branch. Switch branches by using the following command in the terminal

📘 Command to execute

git checkout step-1

Expected output

2. Making a Serverless endpoint using Netlify functions

✅ Step 2a: Check out the functions folder

Each file in our functions folder represents a REST API endpoint implemented as a serverless function (we'll get to this in a moment). For now, take a look at the helloWorld.js file inside the functions folder.

insert

At this point, this REST API endpoint is stubbed out. If we use this as it, it will simply give us back {"hello":"world"}.

exports.handler = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({ hello: "world" }),
  };
};

✅ Step 2b: Test the REST API with a browser

Be sure that the app you had running in the previous step has been shutdown (Ctrl-C). To try the REST API along with the front end, in the terminal use the command:

📘 Command to execute

npm run dev
  • This will give you the UI plus run the helloWorld function in the background.

GitPod

  • See the UI running at: https://8888-<your_uid>.<your_region>.gitpod.io
  • See the end point at: https://8888-<your_uid>.<your_region>.gitpod.io/.netlify/functions/helloWorld

Notice the port, GitPod generated ID, and the GitPod region in the URL below at each arrow. This should automatically be generated for you when you run npm run dev. Just paste /.netlify/functions/helloWorld on to the end in order to get to the correct endpoint.

📗 Expected output

test functions output

LOCAL

  • See the UI running at: localhost:8888
  • See the end point at: localhost:8888/.netlify/functions/helloWorld

You should see this output at the endpoint

{ "hello":"world" }

This is our serverless function giving us back the "Hello World" example.

✅ Step 2c: Run the existing unit tests

✔️ Have a look at the /test/helloWorld.test.js file, this does not do much at this point. This basically tests the helloWorld function to ensure that we get "world" in our response, and hence we would know that the function is working correctly.

✔️ The way we are going to approach writing our tests is by asking the question "What does our endpoint need to do?". We want our function to create a new game on Astra (provision a new game) -- and we provide the API with a random game code so this can work.

const insertGame = require("../functions/insertGame");

it("should return a JSON response", async () => {
  const response = await insertGame.handler();
  const responseJson = JSON.parse(response.body);
  expect(responseJson.hello).toBe("world");
});

Run the test to try it out:

📘 Command to execute

npm run test:functions

Click below to move to the next section.

⚒️ Setup and deploy your first app > 📚 What can Netlify do for you > ⚒️ Create a mock REST API with Netlify functions | next=> 📚 What is DataStax Astra and Stargate