Take your cow anywhere with Azure functions
Hayy!!, Do want to have a pet to go everywhere from your webapps to chatbots to arduino screens to terminals? Let’s code..
no animals were harmed while writing this blog or the code. ;p
beware of some cancerous puns coming at you..
In this blog we will write a server side code which responds to HTTP request with QueryParameters of text which responds with a cow echoing the request, or mooing to be specific ;p
- We are setting up an API (serverside application)which responds with a ASCII art of cow saying what you sent in QueryString .
- npm i -s cowsay
that’s hello in cow language
Being an Indian I recently had a chance to interview cow at my nearest where I booked a place to heaven and got some answers to Q# tools and latest news about taimur .
Cow whispered that she loves bing wallpapers and wants to explore internet.
I thought why not having her everywhere and then I met cowsay.
I thought hay, why not having cow over cloud to let her explore.
Alright let’s get the cow to touch skies.
1. Head over to azure portal and create a function app
2. Create a HTTP trigger function by clicking on new or the ‘+’ sign in the functions tab.
HTTP trigger function means the code is triggered when it gets a HTTP request and function runs the code based on trigger .
I recommend here creating an anonymous function so that we don’t need a token to access our cow. When in production always function authorization level so no one can get hold of your function(s).
Let’s have a look on Code
The first argument every function is passed is a
contextobject which is used for receiving and sending binding data, logging, and communicating with the runtime.
req object in the module.exports is an Object with all request data, be it queryparameters or the body.
Context.res is the object which you want to return as a response.
when we set context.res ascowsay.say with text passed as req.query.text, we tell to return the text in ASCII format of cow echoing the same text passed in request query or body.
save the code and follow up.
Click on your azure function app name (cowfunc here) > Platform features and head over to KUDU.
5. Choose CMD in debug console
6. cd site/wwwroot
wwwroot folder contains all the files in a app service at azure.
Commands now, let’s initialise a npm package so that we can use cowsay.
Fill some details about your package like name, version and author, confirm with yes, and check for the file package.json to be created.
8. let’s save the cowsay package so that he code we wrote has access to cowsay library.
npm i -s cowsay
This saves the cowsay library to node_modules folder. check for the node_modules folder in the directory.
We installed the dependencies.
We wrote the code and fulfilled dependencies, what next?
Get back to your http trigger function and click test on the right side of your blade. This will open a magical door with a way to narnia. nope, just kidding.
Set the HTTP method to GET and in QUERY add text and message as shown in image.
click run and boom there’s a cow.
As now the code is in the cloud you can access it anytime with just sending a HTTP Request ( copy the url in </> get function url option ).
Arduino ESP and Ethernet modules can execute HTTP requests, you might use this in chatbots, you can use this in websites, typically anywhere.
want to use it in your bash terminal?
If you want to use it locally, Azure functions is awesome enough to have local development using cli.
func host start
Awesome in local too.
Well now we know that cow says moo and whatever we pass into queryparameters, but do you know what does the fox say?
Tell me in comments ;p