Skip to main content

Azure Functions

还没翻译译自英文(hono.dev)本页 29 段都还没译

Azure Functions

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 was not designed for Azure Functions at first, but with Azure Functions Adapter, it can run on it as well.

It works with Azure Functions V4 running on Node.js 18 or above.

1. Install CLI

To create an Azure Function, you must first install Azure Functions Core Tools.

On macOS

brew tap azure/functions
brew install azure-functions-core-tools@4

Follow this link for other OS:

2. Setup

Create a TypeScript Node.js V4 project in the current folder.

func init --typescript

Change the default route prefix of the host. Add this property to the root json object of host.json:

"extensions": {
    "http": {
        "routePrefix": ""
    }
}

Note

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

Now you are ready to install Hono and the Azure Functions Adapter with:

npm i @marplex/hono-azurefunc-adapter hono
yarn add @marplex/hono-azurefunc-adapter hono
pnpm add @marplex/hono-azurefunc-adapter hono
bun add @marplex/hono-azurefunc-adapter hono

3. Hello World

Create src/app.ts:

// src/app.ts
import { Hono } from 'hono'
const app = new Hono()

app.get('/', (c) => c.text('Hello Azure Functions!'))

export default app

Create src/functions/httpTrigger.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

Run the development server locally. Then, access http://localhost:7071 in your Web browser.

npm run start
yarn start
pnpm start
bun run start

5. Deploy

Note

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:

npm run build
yarn build
pnpm build
bun run build

Deploy your project to the function app in Azure Cloud. Replace <YourFunctionAppName> with the name of your app.

func azure functionapp publish <YourFunctionAppName>