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..

Ayush
Nov 22, 2018 · 5 min read

no animals were harmed while writing this blog or the code. ;p
beware of some cancerous puns coming at you..

Holy Cow!!

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

Tl;dr

  1. We are setting up an API (serverside application)which responds with a ASCII art of cow saying what you sent in QueryString .
  2. npm i -s cowsay
  3. context.res(cowsay.say());

Mooooo

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

Step 1: Create a function app

And choose JavaScript as Runtime Stack as the code I’ll be showing here is of NodeJS.

2. Create a HTTP trigger function by clicking on new or the ‘+’ sign in the functions tab.

Step 2: Create a HTTP trigger function

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).

3.
Let’s have a look on Code

We will use a cowsay npm module which will help us creating a cow.
you can have a look at cowsay github here.

you can have a look at azure functions javascript docs here.

The first argument every function is passed is a context object 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.

4.

Click on your azure function app name (cowfunc here) > Platform features and head over to KUDU.

Platform features

5. Choose CMD in debug console

KUDU

6. cd site/wwwroot

wwwroot folder contains all the files in a app service at azure.

site/wwwroot

7.

Commands now, let’s initialise a npm package so that we can use cowsay.

npm init
npm init

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.

execute

 npm i -s cowsay
node_modules

This saves the cowsay library to node_modules folder. check for the node_modules folder in the directory.
We installed the dependencies.

9. TESTing

We wrote the code and fulfilled dependencies, what next?
Testingg.

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?

curl https://cowfunc.azurewebsites.net/api/cowfunc

If you want to use it locally, Azure functions is awesome enough to have local development using cli.

download azure function cli from here. Clone this repo and execute following.

npm install
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

Ayush

Written by

Ayush

Copy-Pasta or Copy-Sphegatti idk. https://heyayush.dev/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade