Challenge
Here’s a starter exercise for Azure Functions: write a function to return the current date.
Solution Steps
- The first set of tasks are all around getting a function set up on https://portal.azure.com/
- Log in to the Azure portal
- Make a resource group “learning”. We’ll use this in the future to organise all Azure resources associated with throw-away learning tasks
- Add a “Function App” called “datenow”
- OS = Windows
- Located = UK West
- Create new storage
- In the function app create new function of type “HTTP Trigger” and language JavaScript
- 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(); };
- 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”