未加星标

Bring static to life using serverless

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

S tatic websites really are the latest fashion, and they’re really great and have many benefits. But sometimes you need to provide some dynamic content, store form data, etc. bringing small dynamic widgets/islands of life to otherwise static site.

Here in my opinion Azure Functions is a perfect fit, you can quickly create tailored and easily consumable just pay per use functions, in a variety of languages such as C#, F#, PowerShell, javascript and more.


Bring static to life using serverless

In this post will as an example demonstrate how you on a website can add the last ten Medium blog posts, that also must have tags specified.

We’ll be using the data provided by the users Medium RSS feed, found at https://medium.com/feed/@user .

In this post I’ll assume you’ve got some knowledge of Azure Functions, I’ve placed links to my previous Azure Functions post at the end of this post if you want to learn more on getting started with functions.

Server(less) side

Our Azure Function will download the RSS XML data, select only what’s needed (title, link and date) to display on website and return as easily from JavaScript consumable JSON format, a side effect is that this also results in only a fractions the amount of data downloaded, full control on which items and what data is exposed.


Bring static to life using serverless
First line medium unfiltered RSS feed, second line Azure Functiondata

This also makes it perfect for mobile apps consuming third party APIs where you can save bytes by resulting in a more responsive app and happier users as you’re not eating up their cellular data-plan.

The Function

This function will basically call an RSS feed helper method that returns the mapped data as JSON and then the function returns it as an dynamic JavaScript file, exposing an feed variable.

The helper method is placed in an external file, which makes it easily reusable from other functions.

By design functions aren’t allowed to be cached by the browser, which makes perfect sense in most cases. But by setting the response object Headers CacheControl property, we can override the default behavior, letting the function result be cached for 10 minutes.

Which means while user navigates you site they’ll only hit that functions once every ten minutes, the rest of the time fetched from local cache and this will result in you saving money and the user gets quicker page loads.


Bring static to life using serverless
The helpermethod

As Functions give you the full power of.NET and C# you can with very few lines of code, fetch, parse and map the data how you want. Below I’m using LINQ to XML/objects to parse title, link and published date (formatting it as I want) for items having at least one category specified, then serializing the result to JSON using JSON.Net.

Client side

As the function is exposed a JavaScript there’s no cross site issues, you’ll just use the script tag as you would do when fetching from i.e. a CDN.

The template

In this post I chose a really basic view template engine called mustache.js which is the JavaScript implementation of the mustache template language. It does support supplying object as model, iterating and applying an template to each item in an array.

Just bing an JavaScript object exposed as a variable makes it very easy to use any template engine you want whether it be React, Angular, Aurelia etc.

Azure functions does support CORS and of course just returning JSON consumable as any rest API too.

Result

The end result of the template with look something like below


Bring static to life using serverless
Conclusion

I the cloud the Sky truly is the limit and Azure Functions truly are more versatile than first meets the eye.

The Code

Complete code example for this function can be found on GitHub

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaScriptJavaC#XMLPowerShellGitReactF#GitHubCDN
分页:12
转载请注明
本文标题:Bring static to life using serverless
本站链接:http://www.codesec.net/view/530684.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(61)