⚒️ 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
+ 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
✅ Step 1a: Checkout expected branch
- 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
✅ 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.
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.
- 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
- See the UI running at:
localhost:8888
- See the end point at:
localhost:8888/.netlify/functions/helloWorld
{ "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