Appearance
Azure 函数
¥Azure Functions
Azure 函数 是来自 Microsoft Azure 的无服务器平台。你可以运行代码以响应事件,它会自动为你管理底层计算资源。
¥Azure Functions is a serverless platform from Microsoft Azure. You can run your code in response to events, and it automatically manages the underlying compute resources for you.
Hono 最初并不是为 Azure Functions 设计的。但使用 Azure Functions 适配器 它也可以在它上面运行。
¥Hono was not designed for Azure Functions at first. But with Azure Functions Adapter it can run on it as well.
它适用于在 Node.js 18 或更高版本上运行的 Azure Functions V4。
¥It works with Azure Functions V4 running on Node.js 18 or above.
1. 安装 CLI
¥ Install CLI
要创建 Azure 函数,必须先安装 Azure Functions 核心工具。
¥To create an Azure Function, you must first install Azure Functions Core Tools.
在 macOS 上
¥On macOS
sh
brew tap azure/functions
brew install azure-functions-core-tools@4
其他操作系统请点击此链接:
¥Follow this link for other OS:
2. 设置
¥ Setup
在当前文件夹中创建一个 TypeScript Node.js V4 项目。
¥Create a TypeScript Node.js V4 project in the current folder.
sh
func init --typescript
更改主机的默认路由前缀。将此属性添加到 host.json
的根 json 对象:
¥Change the default route prefix of the host. Add this property to the root json object of host.json
:
json
"extensions": {
"http": {
"routePrefix": ""
}
}
信息
默认的 Azure Functions 路由前缀为 /api
。如果你不按照上面那样改的话,一定要确保所有 Hono 路由都以 /api
开头
¥The default Azure Functions route prefix is /api
. If you don't change it as shown above, be sure to start all your Hono routes with /api
现在你已准备好安装 Hono 和 Azure Functions Adapter:
¥Now you are ready to install Hono and the Azure Functions Adapter with:
sh
npm i @marplex/hono-azurefunc-adapter hono
sh
yarn add @marplex/hono-azurefunc-adapter hono
sh
pnpm add @marplex/hono-azurefunc-adapter hono
sh
bun add @marplex/hono-azurefunc-adapter hono
3. Hello World
创建 src/app.ts
:
¥Create src/app.ts
:
ts
// src/app.ts
import { Hono } from 'hono'
const app = new Hono()
app.get('/', (c) => c.text('Hello Azure Functions!'))
export default app
创建 src/functions/httpTrigger.ts
:
¥Create src/functions/httpTrigger.ts
:
ts
// src/functions/httpTrigger.ts
import { app } from '@azure/functions'
import { azureHonoHandler } from '@marplex/hono-azurefunc-adapter'
import honoApp from '../app'
app.http('httpTrigger', {
methods: [
//Add all your supported HTTP methods here
'GET',
'POST',
'DELETE',
'PUT',
],
authLevel: 'anonymous',
route: '{*proxy}',
handler: azureHonoHandler(honoApp.fetch),
})
4. 运行
¥ Run
在本地运行开发服务器。然后,在你的 Web 浏览器中访问 http://localhost:7071
。
¥Run the development server locally. Then, access http://localhost:7071
in your Web browser.
sh
npm run start
sh
yarn start
sh
pnpm start
sh
bun run start
5. 部署
¥ Deploy
信息
在部署到 Azure 之前,你需要在云基础架构中创建一些资源。请访问有关 为你的函数创建支持 Azure 资源 的 Microsoft 文档
¥Before you can deploy to Azure, you need to create some resources in your cloud infrastructure. Please visit the Microsoft documentation on Create supporting Azure resources for your function
构建项目以进行部署:
¥Build the project for deployment:
sh
npm run build
sh
yarn build
sh
pnpm build
sh
bun run build
将你的项目部署到 Azure Cloud 中的函数应用。将 <YourFunctionAppName>
替换为你的应用名称。
¥Deploy your project to the function app in Azure Cloud. Replace <YourFunctionAppName>
with the name of your app.
sh
func azure functionapp publish <YourFunctionAppName>