未加星标

Rapid Prototyping Web APIs with Azure Functions

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

One of my favourite things about the new Azure Functions service is how easy it is to quickly prototype application ideas. Without needing to provision or pay for a web server, I can quickly set up a webhook, a message queue processor or a scheduled task.

But what if I’m prototyping a web-page and want a simple REST-like CRUD API? I decided to see if I could build a very simple in-memory database using nodejs and Azure Functions.

Step 0 Create a function app

It’s really easy to get started with Azure Functions, but if you’ve not yet used them, you can sign in to functions.azure.com and you’ll be offered the opportunity to open an existing function app or create a new one:


Rapid Prototyping Web APIs with Azure Functions

It’s just a matter of choosing which region you want to host it in. By default this will create a function app on the Dynamic pricing tier, which is what you want since you only pay for what you use, and there’s a generous free monthly grant , so for most prototyping purposes it’s likely to be completely free.

Step 1 Create a new NodeJS Function

From within the portal, select “New Function”, and choose the “Generic Webhook Node” option.


Rapid Prototyping Web APIs with Azure Functions

Give your function a name relating to the resource you’ll be managing, as it will be part of the URL:


Rapid Prototyping Web APIs with Azure Functions
Step 2 Enable more HTTP Verbs

In the portal, under your new function, select the “Integrate” tab, and then choose “Advanced Editor” to let you edit the function.json file directly.


Rapid Prototyping Web APIs with Azure Functions

You’ll want to add in a new “ methods ” property of the “ httpTrigger ” binding, containing all the verbs you want to support. I’ve added get , post , put , patch and delete


Rapid Prototyping Web APIs with Azure Functions
Step 3 Add the In-Memory CRUD javascript File to your Function

I’ve made a simple in-memory database supporting CRUD in node.js. I’m not really much of a JavaScript programmer or a REST expert, so I’m sure there’s a lot of scope for improvement (let me know in the comments), but here’s what I came up with :

To get this into your function you have a few options. The easiest is in the “Develop” tab to hit “View Files”, and drag and drop my inMemoryCrud.js file straight in. Or you can just create a new file and paste the contents in.


Rapid Prototyping Web APIs with Azure Functions

If you look through you’ll see it supports GET of all items or by id, inserting with POST and deleting with DELETE, as well as replacing by id using PUT and even partial updates with PATCH. It optionally lets you specify required fields for POST and PUT, and there’s a seedData method for you to give your API some initial data if needed.

Obviously, since this is an in-memory database there are a few caveats. It will reset every time your function restarts, which will happen whenever you edit the code for your function, but can happen at other times too. Also if there are two servers running your function, they would both have their own in-memory databases, but Azure functions is unlikely to scale up to two instances for your function app unless you are putting it under heavy load.

Step 4 Update index.js

Our index.js file contains the entry point for the function. All we need to do is import our in memory CRUD JavaScript file, seed it with any starting data we want, and then when our function is called, pass the request off to handleRequest , optionally specifying any required fields.

Here’s an example index.js :

Step 5 Enabling CORS

If you’re calling the function from a web-page then you’ll need to enable CORS. Thankfully this is very easy to do, although it is configured at the function app level rather than the function level. In the portal, select function app settings and then choose Configure CORS.


Rapid Prototyping Web APIs with Azure Functions
Rapid Prototyping Web APIs with Azure Functions

In there, you’ll see that there are some allowed origins by default, but you can easily add your own here, as I’ve done for localhost:


Rapid Prototyping Web APIs with Azure Functions
Step 6 Calling the API

The Azure function portal tells you the URL of your function which is based on your function app name and function name. It looks like this:

https://myfunctionapp.azurewebsites.net/api/Tasks?code=bzxkgm4e13gowmm2zo26jq0k9sz8p18k7j1aa5v2mgf9p3pgb9j57uu1tkzwiylvwa9ohb5u3di

The code is Azure Function’s way of securing the function, but for a prototype you may not want to bother with it. You can turn it off by setting authLevel to anonymous in the function.json file for the httpTrigger object.


Rapid Prototyping Web APIs with Azure Functions

The other slight annoyance is that Azure Functions doesn’t support us using nice URLs like https://myfunctionapp.azurewebsites.net/api/Tasks/123 to GET or PUT a specific id. Instead we must supply the id in the query string: https://myfunctionapp.azurewebsites.net/api/Tasks?id=123

Is there a quicker way to set this up?

If that seems like quite a bit of work, remember that all an Azure function is, is a folder with a function.json file and some source files. So you can simply copy and paste the three files from my github gist into as many folders as you want and hey presto you have in-memory web-apis for as many resources as you need just by copying and renaming folders e.g. orders, products, users etc. If you’re deploying via git (which you should be), this is very easy to do.

What if I want persistence?

Obviously in-memory is great for simple tests, but at some point you’ll want persistence. There are several options here, including Azure DocumentDb and Table Storage, but this post has got long enough, so I’ll save that for a future blog post.

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: JavaScriptJavaRESTUT
分页:12
转载请注明
本文标题:Rapid Prototyping Web APIs with Azure Functions
本站链接:http://www.codesec.net/view/481098.html
分享请点击:


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