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@4Follow this link for other OS:
2. Setup
Create a TypeScript Node.js V4 project in the current folder.
func init --typescriptChange the default route prefix of the host. Add this property to the root json object of host.json:
"extensions": {
"http": {
"routePrefix": ""
}
}INFO
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 honoyarn add @marplex/hono-azurefunc-adapter honopnpm add @marplex/hono-azurefunc-adapter honobun add @marplex/hono-azurefunc-adapter hono3. 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 appCreate 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 startyarn startpnpm startbun run start5. Deploy
INFO
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 buildyarn buildpnpm buildbun run buildDeploy your project to the function app in Azure Cloud. Replace <YourFunctionAppName> with the name of your app.
func azure functionapp publish <YourFunctionAppName>