我在请求我的 api 时收到错误 404 Not Found。我不知道如何正确编写代码。这是我的代码示例:
主机.json:
{
"version": "2.0",
"extensions": {
"http": {
"routePrefix": ""
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
local.settings.json:
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
}
index.ts:
import { app } from "@azure/functions";
app.http("hello", {
methods: ["GET"],
authLevel: "anonymous",
route: "hello",
handler: helloController.hello.bind(helloController),
});
你好控制器:
import { NextFunction, Request, Response } from "express";
class HelloController {
public async hello(req: Request, res: Response, next: NextFunction) {
try {
return res.sendStatus(200).json("Hello World");
} catch (e) {
next(e);
}
}
}
export const helloController = new HelloController();
.yml:
name: Build and deploy Node.js project to Azure Function App - buxonline
on:
push:
branches:
- node_version
workflow_dispatch:
env:
AZURE_FUNCTIONAPP_PACKAGE_PATH: 'backend' # set this to the path to your web app project, defaults to the repository root
NODE_VERSION: '20.x' # set this to the node version to use (supports 8.x, 10.x, 12.x)
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 'Checkout GitHub Action'
uses: actions/checkout@v4
- name: Setup Node ${{ env.NODE_VERSION }} Environment
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
- name: 'Resolve Project Dependencies Using Npm'
shell: bash
run: |
pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
npm install
npm run build --if-present
npm run test --if-present
popd
- name: Zip artifact for deployment
run: zip release.zip ./* -r
- name: Upload artifact for deployment job
uses: actions/upload-artifact@v3
with:
name: node-app
path: release.zip
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
permissions:
id-token: write #This is required for requesting the JWT
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v3
with:
name: node-app
- name: Unzip artifact for deployment
run: unzip release.zip
- name: Login to Azure
uses: azure/login@v1
with:
client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_689801BD1C5E47F3B0BE98C1A283FD1E }}
tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_5D60349DAA2042FEB1925BC25A5A1309 }}
subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_DB7120F777A048AFAF9B033F5A591B7A }}
- name: 'Run Azure Functions Action'
uses: Azure/functions-action@v1
id: fa
with:
app-name: 'buxonline'
slot-name: 'Production'
package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}
我的项目结构:
-项目根目录:
.github
工作流程
后端
.env
.eslintrc.js
主机.json
local.settings.json
package.json
tsconfig.json
源代码
配置
常数
控制器
枚举
错误
中间件
型号
路由器
服务
静态
类型
app.ts,
index.ts
距离
我像平常的异步函数一样编写,像天蓝色函数一样编写。尝试了处理程序中的一些方法,但没有任何改变
本地运行项目:您应该能够在本地控制台中看到函数触发器,否则部署到 Azure Function App 后不会同步触发器。
示例代码:
import { BaseController } from './BaseController.js'
import { HttpRequest, HttpResponseInit } from '@azure/functions'
export class HelloController extends BaseController {
/**
* Handle the incoming request
* @param request
*/
public async index(request: HttpRequest): Promise<HttpResponseInit> {
const name = request.query.get('name')
return {
status: 200,
jsonBody: {
message: name ? `Hello, ${name}!` : 'Hello World!'
}
}
}
}
编写代码以返回 Http 响应。
import type { HttpResponseInit } from '@azure/functions'
export abstract class BaseController {
protected abort(code: number, message: string = ''): HttpResponseInit {
return {
status: code,
body: message
}
}
}
文件夹结构:
|---.vscode
|---node_modules
|---.deployment
|---.eslintrc.js
|---.funcignore
|---.gitignore
|---host.json
|---local.settings.json
|---package.json
|---package-lock.json
|---tsconfig.json
\---src
+---app
| \---Http
| \---Controllers
| BaseController.ts
| HelloController.ts
|
+---bootstrap
| app.ts
|
+---routes
| api.ts
|
\---services
+---Cache
| CacheManager.ts
| contracts.ts
| exceptions.ts
| MemoryStore.ts
| Repository.ts
|
+---Container
| Container.ts
| contracts.ts
| exceptions.ts
| types.d.ts
|
\---Route
contracts.ts
Route.ts
传送门: