- Open VS Code
- Go to _git folder
Create your first Serverless Function using VS Code
-
Make a folder named first-serverless-api
-
Open the folder in VS Code
-
Install the extension Azure Functions (Reload if needed)
If you do not have the Azure Functions Core Tools installed, you will be automatically prompted to install. Follow the specified instructions.
-
Open Command Palette
-
Enter and select Azure Functions: Create New Project
-
Select the folder first-serverless-api
-
Select JavaScript
-
Select HTTP Trigger
-
Enter the name of the function as products-get
-
Select Anonymous for the Authorization Level
- Open Command Palette
- Enter and select Terminal: Create New Integrated Terminal
- In the terminal, type
func start
- Open your browser (Edge)
- Navigate to http://localhost:7071/api/products-get/?name=John
Walkthrough the directory structure
- Open the folder first-serverless-api in VS Code
- The products-get folder is where the your first function was created
- The
function.json
file is where you can configure how your function is triggered and behaves. - The
index.js
file contains the logic for your function. sample.dat
is a placeholder file for sample data.- Open the file
.vscode/launch.json
to see your debugging configuration - All files listed in
.funcignore
will not be published to Azure - All files listed in
.gitignore
will be ignored by git - The
host.json
metadata file contains global configuration options that affect all functions for a function app. - The
local.settings.json
file stores app settings, connection strings, and settings used by local development tools. - The
package.json
file is where your npm scripts and npm package dependencies are listed. - The
proxies.json
file is where you can specify endpoints on your function app that are implemented by another resource. Which can be used to split a large API into multiple function apps whule still presenting a single API surface.
Update the function to get a list of products
- Open the folder first-serverless-api in VS Code
- Create a file
product-data.js
- Paste the snippet data-products for the data
- Open the
products-get/index.js
file - Delete all of the code
- Paste the snippet func-products for getting products
- Examine the code and the require statement
- Open the file
products-get/function.json
- Remove the post from the methods array
- Add the property for
route: 'products'
- The endpoint will now be triggered only on a GET request to api/products
- Open Command Palette
- Enter and select Terminal: Create New Integrated Terminal
- In the terminal, type
func start
- Open your browser (Edge)
- Navigate to http://localhost:7071/api/products
Debugging a Serverless Application
- Open the folder first-serverless-api in VS Code
- Open the file
.vscode/launch.json
to see your debugging configuration - This configuration was created for you when you created the Function project
- Open the
products-get/index.js
file - Set a breakpoint in the gutter on the line of code that gets the products
- Click the Run and Debug icon in the Activity Bar
- The Run view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.
- Select Attach to Node Functions
- Click the green arrow to debug (this will start the functions app)
- Open your browser (Edge)
- Navigate to http://localhost:7071/api/products
- Examine how the debugger stops on your breakpoint
- Everythign about debugging is here in the Run and Debug view
- Explore the variables windows
- Explore the Local and Closure lists
- Hover over the
products
array, notice how it is undefined - Step over the line of code.
- Hover over the
products
array and notice how it changed - Expand the variables
- Point out the Watch, Call Stack, and Breakpoints panels
- Tell the debugger continue to run
- Stop the debugger
Install Node Modules in your Serverless Function
Update the function to format the expiration dates
- Open the folder first-serverless-api in VS Code
- Open Command Palette
- Enter and select Terminal: Create New Integrated Terminal
- In the terminal, type
npm install date-fns
- Examine
package.json
to see the new dependency
- Open the
products-get/index.js
file - Import
date-fns
by pasting the snippet func-products-date-fns - Examine the code and the require statement
- Open the
products-get/index.js
file - Change the variable from
products
torawProducts
when retrieving the data - Create a new line of code beneath the
const rawProducts
- Paste the snippet func-products-format to format the dates with YYYY-MM-DD in a new array named
products
- Examine the array map function and the spread operator logic briefly
- Open Command Palette
- Enter and select Terminal: Create New Integrated Terminal
- In the terminal, type
func start
- Open your browser (Edge)
- Navigate to http://localhost:7071/api/products