In this lesson, you will learn how to put your Netlify Functions in action without having to deploy them on the Internet. We will make use of the Netlify CLI to run our application and invoke the Netlify Functions directly on your computer.
Duration
2 min
Access
Free
Transcript
Available
In this lesson, you will learn how to put your Netlify Functions in action without having to deploy them on the Internet. We will make use of the Netlify CLI to run our application and invoke the Netlify Functions directly on your computer.
Video Transcript
Prince Wilson: To get started, we're going to build a netlify.toml file. Inside of here, we're going to declare, during the build phase, their functions are located in the functions directory.
Currently, we don't have that existing, so let's make sure we're going to add that. We want this at the top most level. Let's add a folder called functions. Inside of that folder, we're going to declare a file called hello.js.
In this file, we must have exports.handler equal to async function with two parameters -- event and context. We won't be using this in this video, but it's important to know that they are there.
This function needs to return a json object with a key of statusCode declaring, "Hey, everything is OK," so 200. The key body, the body of the response, so in our case, "Hello, World." Congratulations, you made a Netlify function.
Go into your terminal. Write the command, netlify dev. This will run the application as well as get everything set up for Netlify. In another terminal session, make sure to run the command, netlify functions:invoke. This will invoke our function locally. Inside of the terminal, you should see the output, "Hello, World."
To recap, we started by creating a netlify.toml file where we defined where Netlify should build a function. We went ahead and build our first function inside up here returning the body, "Hello, World."
We went inside of our terminal and wrote the command, netlify dev. We see the application has started our local dev server. We opened another terminal session, wrote netlify functions:invoke and saw the output of our function, "Hello, World."