The goal of this tutorial is to guide you through the full process of implementing Azure function, with Node.js as a development language and VS Code as a source code editor. Also, we will help you learn how to use the “Run from package” feature to improve performance in Node.js functions and overcome cold-start delays. We talk more about this in the blog: Improving the performance of Node.js Azure functions.

Creating Functions From Scratch

For this example, we will use VS Code with Azure extensions and the most recent update package. In this blog, we will also discuss the tools you need to install before you start coding Azure functions. First, we will create a new “Function App” in the Azure portal and select VS Code as a development environment.

To do this, we need to fill some parameters:

  • App name: This will be the final name for the functions group. It also affects the final URL. In this example, I use functions: nodejs.azurewebsites.net.
  • Subscription: Choose your subscription.
  • Resource group: Select one if you have a previous configured resource group, and the function app belongs to the same permissions.
  • OS: Windows is the default option. For this example, though, I will use Linux.
  • Publish: Select the code.
  • Hosting plan: Choose the consumption plan.
  • Location: Choose the location you prefer.
  • Runtime Stack: Node.js is ideal.
  • Storage: You can create a new one if you prefer, or select a previously created storage account.
  • Application Insights: This is used to detect errors and gas logs.

Node.js Azure Functions With VS Code

After you click ‘Create,’ you will need to wait until it has been created. When it’s ready, you’ll receive a notification.

Node.js Azure Functions With VS Code 3

Once you’ve created the function app, it’s time to start coding. In this case, we will create a new function and select VS Code as the development environment.

Node.js Azure Functions With VS Code

After this, you’ll need to install VS Code, Node.js, .NET Core 2.1, and Azure Functions Core Tools. You should follow the same installation order mentioned previously:

Node.js Azure Functions With VS Code

How to Create Node.js Azure Functions With VS Code

We also recommend installing these extensions:

  • Azure Account: This allows integration between Azure and VS Code.
  • Azure Functions: This allows you to create, debug, manage, and deploy functions.

Node.js Azure Functions With VS Code

The next step you need to do is to authenticate with Azure Account, so you will see your functions there.

Node.js Azure Functions With VS Code

Now, you can create functions using the Azure Functions extension. For this example, we will create an HTTP trigger:

Node.js Azure Functions With VS Code

Next, you have to enter a function name and select ‘Function’ as an authorization level.

Node.js Azure Functions With VS Code

Node.js Azure Functions With VS Code

At this point, you’ve created your function. The main file where you include your code is index.js. As you can see in the image below, it includes a shortcode which receives a query.name or body.name and returns “Hello ” + (req.query.name || req.body.name), otherwise it will return a 400 error message.

Node.js Azure Functions With VS Code

Another important file is function.json. This includes some configurations, the most important of which are the methods chosen for support, so you can set GET, POST, PUT, DELETE, OPTIONS.

Node.js Azure Functions With VS Code

Testing Your Function and Deploying in Azure

If you debug or test locally, you should put a breakpoint in the code and press F5.

Node.js Azure Functions With VS Code

Doing this will start a localhost server:

Node.js Azure Functions With VS Code

As an example, this case is http://localhost:7071/api/nodeFunction1?name=Paul. We’re sending a ‘name’ parameter. If we don’t, it will return an error message.

Node.js Azure Functions With VS CodeNext, you’ll want to deploy your function. To do this, right-click over the function name and select ‘Deploy to Function App…’

Select the function app you prefer. In this example, it’s ‘functions-nodejs.’

Node.js Azure Functions With VS Code

Node.js Azure Functions With VS Code

Node.js Azure Functions With VS Code

If there are no errors, you will see a success message:

Node.js Azure Functions With VS Code

Run From Package Configuration

Once you’ve created and deployed functions, you can activate the ‘Run From Package’ option in the Application settings. This creates a zip file (which includes all necessary code) every time it’s deployed. In short, this step makes the file faster to read and load, while also helping to avoid long cold-start delays.

Node.js Azure Functions With VS Code

At that point, the deployed function should be uploaded to/data/SitePackages in zip format, although it is automatically uploaded when you deploy from VSCode.

Node.js Azure Functions With VS Code

Once you’ve done all this, you should be able to use VSCode to manage Azure functions easily. Thanks to the full integration, which you can use to create or deploy functions, you can also test locally to identify possible issues. Another good improvement from Azure is the “Run from package” option, which allows substantial performance improvement for Node functions.