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:
-
Create Redis server in Azure
-
Create API App in App Services
-
Create Node.js Web API in Visual Studio
-
Publish Node.js API in Azure App Services
-
Test API with Rest Client
Download code here:
Https://github.com/Kodran/Redis-Geospatial-WebAPI
Pre requirements
- Azure subscription, you can get a free trial here
- Visual Studio 2013+
- Node.js and npm
- Node.js Visual Studio Tools
- Node.js Redis Client
- Npm install redis
-
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:
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:
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
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:
-
ExpressJS
-
Redis
-
Body-parser
Express:
Redis:
Body-parser:
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:
Web API:
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
We chose "Microsoft Azure App Services" and then chose the resource corresponding to our API, in our case is: " api-node-demo "
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:
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:
-
Host
-
Port
-
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.
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
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
CoffeeShopAPI.js
Now we can test our API with some RestClient, in my case I used Postman.
POST Method:
Now we can see our data in Redis with “Redis Desktop Manager”:
GET Method
With this method we will prove that our record was successfully inserted
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
GetGeoRadius method
InsertGeoLocation method
CoffeeShopAPI.js
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
In Postman we enter the URL of our API in Azure and we test the two methods that we have.
Insert coffee shop
Consult nearby cafeteria (1000 meters around)
Insert another cafeteria
Consult nearby cafeteria (1000 meters around)
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
Post a Comment