Geospatial queries in Azure Redis Cache using Node.js Azure API App

Hello world!

Now I come with a guide on how to perform geospatial queries in Azure Redis Cache using Redis for Node.js.

To create our environment, we will perform the following steps:

  1. Create Redis server in Azure

  2. Create API App in App Services

  3. Create Node.js Web API in Visual Studio

  4. Publish Node.js API in Azure App Services

  5. Test API with Rest Client

Download code here:

Https://github.com/Kodran/Redis-Geospatial-WebAPI

Pre requirements

Create Redis server in Azure

Enter to the Azure portal and look for: "Redis Cache", then add a Redis server and capture the data of the new server:

Image

Referring to Fig.

For demonstration reasons, we will choose a standard server, remember that if we want to configure a Redis cluster or make more advanced configurations, we need to acquire a "Premium" server, for more information you can visit the following link:
https: // docs. Microsoft.com/en-us/azure/redis-cache/cache-premium-tier-intro

Once our Redis server is created, we will create our App API resource.

Create Azure API App Resource

Go to the Azure portal and look for API App:

Image

10

Create Node.js Web API in Visual Studio

Create new Node.js project as Azure API App in Visual Studio:

Templates -> JavaScript -> Node.js -> Blank Azure Node.js Web Application

Image

Once we have the project created, we will download npm the necessary packages to make the connections to Redis and we will use Express.js to develop our Web API in Node.js

NPM Packeges:

  1. ExpressJS

  2. Redis

  3. Body-parser

2

Express:

Image

Redis:

Image

Body-parser:

Image

Node.js and Web API Server

To create our Node.js server and expose our Web API, we will insert the following code block:

Server:

Image

Web API:

Image

We create a new folder called "Services" and then a Javascript file (JS) to write our Web API

The function of our code is to initialize express and then import our Web API module from CoffeShop and then expose it by the port that Azure provides us or by port 8080.

Publish Node.js API to Azure API App

To test that our API works correctly in Azure, we will publish in Azure by right clicking on the project:

Redis.Geospatial.WebAPI -> Publish

Image

We chose "Microsoft Azure App Services" and then chose the resource corresponding to our API, in our case is: " api-node-demo "

Image

Image

Image

At the end we click on "Publish" and wait for Visual Studio to deploy in Azure.

Once Visual Studio does the deployment, we will test our API in the browser:

Image

Connect Web API to Azure Redis Cache

To connect to Redis Cache and queries, we will obtain the connection data and then enter them into our API:

  1. Host

  2. Port

  3. Auth key

For the sake of simplicity, we will deactivate Redis SSL port, so we can connect to port 6379.

Note: Redis defaults to port 6380 which is reserved for SSL requests, to deactivate the SSL port, it is necessary to go to "Ports" in our Redis Cache in the Azure portal and then disable SSL access, this will enable port 6379 for non-SSL requests.

Image

Image

Since we have our login information, we are going to add a new file, which will expose our access methods to Redis.

We create the following file:

Redis.Geospatial.WebAPI -> Cache -> RedisCacheAPI.js

And we write the following code:

RedisCacheAPI.js

Image

Explanation:

All the connection data is entered to create our client. We then enter our authorization key to make requests to our Redis server.

We create two prototypes of our redisApi object to expose it in the module.

Then we will import our API into the server to get a single connection per instance and then we pass the Redis client to whatever module we want, in our case we will pass it to our CoffeeShopAPI

Server.js

Image

CoffeeShopAPI.js

Image

Now we can test our API with some RestClient, in my case I used Postman.

POST Method:

Image

Now we can see our data in Redis with “Redis Desktop Manager”:

Image

GET Method

With this method we will prove that our record was successfully inserted

Image

Add methods to insert and consult nearby coffee shops

In order to consult geolocation data, it is necessary to insert a spatial data structure in Redis, for this we will use our Redis API that we created previously so that it now saves spatial data.

RedisCacheAPI.js

Image

GetGeoRadius method

Image

InsertGeoLocation method

Image

CoffeeShopAPI.js

Image

Publish and test Web API

To test that our API works correctly, we will publish it in Azure by right clicking on the project

Redis.Geospatial.WebAPI -> Publish -> api-node-demo

Image

In Postman we enter the URL of our API in Azure and we test the two methods that we have.

Insert coffee shop

Image

Consult nearby cafeteria (1000 meters around)

Image

Insert another cafeteria

Image

Consult nearby cafeteria (1000 meters around)

Image

Success!! We have been able to perform geospatial queries in Redis using Node.js, now you can add and search nearby coffee shops for a given point.

The advantage of our code is that we can add another API for example: "Restaurants, businesses, etc" and later we only refer to our RedisAPI module and we can already perform operations in cache.

I guess this will give you lots of ideas to develop super cool apps!

Download code here:

Https://github.com/Kodran/Redis-Geospatial-WebAPI

Regards!

Comments

Popular posts from this blog

Configurar y desplegar una Web API en Azure App Service Environment

Ejecutar pruebas de volumen con Visual Studio y Team Service

Como crear una aplicación .NET Core y almacenarla en un contenedor de Docker usando Visual Studio

Integrar Web API de .NET en proyecto de Xamarin.Forms utilizando Visual Studio y Azure API App