0%

前言

Azure functions之前先来说说Serverless。Serverless是最近很火的一个概念,它是一种基于互联网的技术架构理念,应用逻辑并非全部在服务端实现,而是采用FAAS(Function as a Service)架构,通过功能组合来实现应用程序逻辑。应用业务逻辑将基于FAAS架构形成独立为多个相互独立功能组件,并以API服务的形式向外提供服务;很多公司都在自己的云平台推出了FAAS,比如Amazon Lambda,Azure Function,Google Cloud Functions,阿里云Function Compute等。

微软的Azure functions是基于Azure的无服务器计算服务,可以快速的帮助用户构建一个基于事件驱动的应用架构。用户无需关心服务器的部署运营,只需专注于核心的逻辑,既可以发布上线。费用按照实际调用次数与时长计费,自动缩放,仅在运行函数时为计算资源付费。更棒的是Azure Functions支持C#,Python,Javascript,TypeScript,F#和Java,本文带大家快速看在本地搭建Javascript开发环境开发Azure Functions。

环境安裝

可以使用Visual Studio CodeVisual Studio开发,本文将会使用的VS Code。

首先在扩展市场中搜索Azure functions,你也可以直接安裝Azure App Service,这样你就拥有所有相关扩展服务,也可以仅安裝Azure Functions。

Azure Function扩展

接著,我们要安裝 Azure Function Core Tools,在命令行安装:

1
2
3
4
5
6
7
8
9
10
#via npm
npm i -g azure-functions-core-tools --unsafe-perm true

#Linux
wget -q https://packages.microsoft.com/config/ubuntu/19.04/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

#Mac
brew tap azure/functions
brew install azure-functions-core-tools

或者我们可以点击Debug | Run, VS Code会自动提示安装Azure Function Core Tools,确定后自动执行上方命令。

Azure Function Core Tools 自动安装

安裝成功之后,我们可以在命令行输入 func 来检查是否安装成功。

Azure Function Core Tools 安装成功

出现 闪电 就说明安装成功了!

##预告

下一篇博文将会讲到 如何快速部署Azure Function

普通数组:

1
let fruits = [`bananas`, `Apples`, `Oranges`];

很简单,一行搞定:

1
fruits.sort();

但是要注意数组中不同字符串的大小写不一致… 大写的字符会被排在小写字符之前😂
所以还有其他的步骤:

1
2
3
4
5
6
7
let fruits = [`bananas`, `Apples`, `Oranges`];
fruits.sort((a, b) => {
return a.toLowerCase().localeCompare(b.toLowerCase());
})
console.log(fruits);

// ["Apples", "bananas", "Oranges"]

对象数组(按对象键值排序)

对对象数组的排序会变得很稍微更复杂一些。经常我们会在处理JSON API的时候遇到。

1
2
3
4
5
6
7
8
9
10
11
let fruits = [
{
fruit: `Bananas`
},
{
fruit: `apples`
},
{
fruit: `Oranges`
}
];

我们可以为此写一个排序函数,但是更进一步我们需要一个更通用的方法把需要排序的键以参数的形式传进去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const propComparator = (propName) =>
(a, b) => a[propName].toLowerCase() == b[propName].toLowerCase() ? 0 : a[propName].toLowerCase() < b[propName].toLowerCase() ? -1 : 1
So now we can use it to sort:

fruits.sort(propComparator(`fruit`));
console.log(fruits);

/*
[
{fruit: "apples"},
{fruit: "Bananas"},
{fruit: "Oranges"}
]
*/

普通对象

如果我们有个对象是这样的:

1
2
3
4
5
let fruits = {
Bananas: true,
apples: false,
Oranges: true
};

我们依然可以将那些键小写化,然后我们先排序键的数组,然后用排序好的键数组建一个新对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let sortedFruits = {};
Object.keys(fruits).sort((a, b) => {
return a.toLowerCase().localeCompare(b.toLowerCase());
}).forEach(function(key) {
sortedFruits[key] = fruits[key];
});
console.log(sortedFruits);

/*
{
apples: false,
Bananas: true,
Oranges: true
}
*/

对象数组(按对象键排序)

1
2
3
4
5
6
7
8
9
10
11
let fruits = [
{
Bananas: true
},
{
Apples: false
},
{
oranges: true
}
];

可能这是上述情况中最复杂,但是结合所有的方法,其实也很简单🐶