Exercise: Write an Azure Function in JavaScript to Return the Current Date

Challenge

Here’s a starter exercise for Azure Functions: write a function to return the current date.

Solution Steps

  1. The first set of tasks are all around getting a function set up on https://portal.azure.com/
  2. Log in to the Azure portal
  3. Make a resource group “learning”. We’ll use this in the future to organise all Azure resources associated with throw-away learning tasks
  4. Add a “Function App” called “datenow”
    • OS = Windows
    • Located = UK West
    • Create new storage
  5. In the function app create new function of type “HTTP Trigger” and language JavaScript
  6. When the app is created it adds a helpful example function. A small amount of modification results in the function returning the current date:
    module.exports = function (context, req) {
        context.log('JavaScript HTTP trigger function processed a request.');
        const now = new Date()
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: now
        };
        context.done();
    };
    

     

  7. Notice how the Azure functions framework handles the encoding of the JavaScript Date object correctly based on the content negotiated with the requestor. Using a HTTP testing tool, like Postman for Chrome, if you specify “Accept: text/json” then you get a JSON represnation back; specifcy “Accept: text/xml” and XML is returned

 

One thought on “Exercise: Write an Azure Function in JavaScript to Return the Current Date

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s