Wednesday, April 13, 2016

Azure Function (JavaScript)


What Is An Azure Function? 


It is the equivalent of the Amazon Lambda. Azure can be written in JavaScript/NodeJs or in C# (Using dynamic C# or ScriptCS syntax). There are different ways to trigger a function
  • On demand via an http (HttpTrigger)
  • Periodically (TimerTrigger)
  • Trigger by blob storage, queue or event hub creation

Learn more : Create Your First Azure Function.
This is the default source code for a Http triggered function.

module.exports = function(context, req) {

    context.log('Node.js HTTP trigger function processed a request. RequestUri=%s', 
                req.originalUrl);

    if (req.query.name || (req.body && req.body.name)) {

        context.res = { 
            status: 200, 
            body: "Hello " + (req.query.name || req.body.name) };
    }
    else {

        context.res = { 
            status: 400, 
            body: "Please pass a name on the query string or in the request body" };
    }
    context.done();
};


Code Structure

Here is my attempt to write a Azure Function and build some re-usable code.
function AzureFunctionBaseClass(context, req) {

    this._context = context;
    this._req     = req;

    this.__terminate = function(m, status) { 

        this._context.res = { status: status, body: m };
        this._context.done();
    }
    this.succeed        = function(m) {  this.__terminate(m , 200); }
    this.fail           = function(m) {  this.__terminate(m , 400); }
    this.getQueryString = function()  { return this._req.query;           }
    this.getBody        = function()  { return this._req.body;            }
    this.log            = function(s) { this._context.log('[log]%s', s);  }
    this.getParameter   = function(n) { return this.getQueryString()[n];  }

    this.run = function() {

        var s = "Inherited run "+(new Date());
        this.log(s);
        this.succeed(s);
    }
    this.__init__ = function() {
        
        this.log("__init__ running");
    }
}

AzureFunctionBaseClass.create = function(functionType, context, req) {

    var l = new AzureFunctionBaseClass(context, req);
    functionType.call(l, context, req);
    l.__init__();
    return l;
}

function MyAzureFunction(context, req) {

    this.run = function() {

        var s = "Hello " + this.getParameter("name");
        this.log(s);
        this.succeed(s);
    }
}

module.exports = function(context, req) {

    AzureFunctionBaseClass.create(MyAzureFunction, context, req).run();
}


How to call the Lambda Function from CURL.exe

C:\Tools\PortableGit\bin>curl.exe -i -X GET "https://blankfunction.azurewebsites.net/api/HttpTrigger
NodeJS1?code=btguk5d3qu2dbvphut20c6jemi9745qootvk9pwwb61fo8byb98ugt79drusztwfjtahu6p3nmi&name=Freddy
"
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 12
Content-Type: text/plain; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=1d89995dbeffc7cc6215c40b5a2e03653fa822c763f464b0126bbb6bc29cc2d8;Path=/;Doma
in=blankfunction.azurewebsites.net
Date: Wed, 13 Apr 2016 12:54:59 GMT

Hello Freddy
C:\Tools\PortableGit\bin>

2 comments:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Node JS Online Training from India . or learn thru Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Online Training

    ReplyDelete
  2. Thanks for providing your information. Keep update with us and share new things Azure Online Training

    ReplyDelete