Create your first Azure function in Nodejs

Meiyappan Kannappa
3 min readSep 1, 2019

Serverless computing or Functions (FaaS) is a cloud computing model which aims to abstract server management and low-level infrastructure decisions away from developers. In this model, allocation of resources is managed by the cloud provider instead of the application architect, which can bring some serious benefits.

Characteristics are

  1. It is provision less
  2. Opaque ( Underlying technology and architecture is not visible)

Scenarios where Serverless will be fit

  1. Unpredictable traffic to your application
  2. Event driven
  3. Small task or Small execution to happen in sporadic manner

We will develop a function just to encrypt and decrypt data that is provided, as needed when executed, we will create this function using nodejs and deploy in Azure Functions.

Pre-Requisites:

  1. Install Microsoft Visual code from here. You can use editor of your choice or refer Azure Functions Quick Start site for other options.
  2. Nodejs — You can install nodejs from here . It comes with default npm. npm is package manager for nodejs libraries

Create a Function App

With visual studio code Press F1 and select “create new project” and follow the instructions, which include selection of programming language (choices are Java/Javascript/C#) and then press F1 to “Create New Function”. This will create a new function with boot strapping code. I have chosen Javascript to generate the below .

Try running the function locally by hitting F5. It will execute the code locally and will provide the URL of the function app running locally.

Now that we have successful in our setup of function app. Next we need to add logic to encrypt and decrypt data. The folder structure created does not have package-json. How to install npm modules? Perhaps we need npm module to encrypt and decrypt data.

Well, Just create a package.json file manually or do “npm init” in the terminal on main function folder, this will initialize npm project with package.json. Done!!!

Next install the cryptr module which is required for this tutorial

npm install cryptr — save

This will install the node module cryptr, required for our tutorial. With this we will encrypt and decrypt the data with function app as below

const encryptedString = cryptr.encrypt(req.query.data);

console.log(encryptedString);

const decryptedString = cryptr.decrypt(req.query.data);

console.log(decryptedString);

Hit F5 to test the code locally

Below are the URLs generated and used in my local dev laptop, if you use diff function app name it may change.

Encrypt: http://localhost:7071/api/MyFunction?type=encrypt&data=helloworld

Decrypt: http://localhost:7071/api/MyFunction?type=decrypt&data=6129ae1252a7666e31f9f11575148e70d3f992afbf648e586512

All set. Now we need to deploy the function app in Azure cloud. Login to Azure cloud from visual studio code.

Once done, you can hit F1 and choose “Deploy Function app” and follow on screen instructions. This will deploy and and provide function app URL in output tab of Visual Studio Code

Complete code can be found in github here

--

--

Meiyappan Kannappa

Technical enthusiast, daydreamer. Design of involute software architecture for digital transformation, sustainability and cost optimization.