Azure Functions are Microsoft’s answer to “serverless” computing. Functions enable applications developers to write event-driven code (ie. “functions”) than can be triggered by timers, manual integration, HTTP requests, service buses, message queues and many other integration points. These functions can be written in a number of different languages including C#, NodeJS (which this example uses), Python, PHP and a myriad of of other languages.
For this example we’re going to setup a simple Azure function that can serve up static files, and thus a website. Combined with other functions, you can create Single Page Apps all served up from Azure Functions without the need for VM’s or other more complex hosting options.
var fs = require("fs")
var mime = require('mime-types')
module.exports = function (context, req) {
var file="index.html"
if (req.query.file) {
file=req.query.file
}
file = file.replace(///g, "");
fs.readFile(__dirname + "content" + file, function(err, data) {
context.log('GET ' + __dirname + "content" + file);
if (!err){
var contentType = mime.lookup(file)
context.res = {
status: 200,
body: data,
isRaw: true,
headers: {
'Content-Type': contentType
}
};
}else{
context.log("Error: " + err)
context.res = {
status: 404,
body: "Not Found.",
headers: {
}
};
}
context.done()
});
};
> npm install mime-types
D:homesitewwwrootGetPage
npm WARN enoent ENOENT: no such file or directory, open 'D:homesitewwwrootGetPagepackage.json'
npm WARN GetPage No description
npm WARN GetPage No repository field.
npm WARN GetPage No README data
npm WARN GetPage No license field.
https://mystaticsite.azurewebsites.net/api/GetPage?file=index.html
This, however, will not work for most static sites, which use a folder structure to point to content. In this case, you can setup a proxy that can transform a URL from…
https://mystaticsite.azurewebsites.net/index.html
… into the URL used by the function. To do this, the + icon next to Proxies in your Function App. Then, Name the “Page Proxy”. For Route template, enter “/{*page}” without the quotes, then “https://localhost/api/GetPage?file={page}” without the quotes for the Backend URL. Finally, click Create.
Functions for sites can be an ideal solution depending on the context. There are certainly other ways to host static sites on Azure. For serverless apps on Azure though, this might be ideal, because you can have other functions that act as API’s that can be called from static context.
You can see a working example using this script at:
https://mystaticsite.azurewebsites.net/
Stay tuned for Part 2!
Cloud management is difficult to do manually, especially if you work with multiple cloud…
Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…
https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…
FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…
Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…
In the intricate landscape of modern business, compliance is both a cornerstone of operational integrity…
View Comments