You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Azure Function自定义API端点并按功能划分项目结构

按功能分组Azure Functions的解决方案

1. 按功能目录重构项目结构

先把9个函数按Feature A/B/C分组,每个功能组单独创建目录,将对应函数的文件夹移入,结构示例如下:

YourFunctionProject/
├── FeatureA/
│   ├── FunctionA1/
│   │   ├── function.json
│   │   └── index.js (或你的代码文件)
│   ├── FunctionA2/
│   │   ├── function.json
│   │   └── index.js
│   └── FunctionA3/
│       ├── function.json
│       └── index.js
├── FeatureB/
│   ├── FunctionB1/
│   ├── FunctionB2/
│   └── FunctionB3/
├── FeatureC/
│   ├── FunctionC1/
│   ├── FunctionC2/
│   └── FunctionC3/
├── host.json
└── local.settings.json

此步骤完成物理层面的功能分组,后续配合路由配置实现URL层面的分组。

2. 为每个函数单独配置路由前缀

放弃host.json的全局routePrefix配置,转而在每个函数的function.json中,为HTTP触发器添加route属性,给不同功能组的函数设置对应前缀:

以FeatureA下的FunctionA1为例,修改其function.json的bindings部分:

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["get", "post"],
      "route": "feature-a/function-a1"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

同理,给FeatureB的函数设置route: "feature-b/function-b1",FeatureC的函数设置route: "feature-c/function-c1"。

配置完成后,本地启动项目,访问路径将变为:

  • FeatureA函数:http://localhost:7071/api/feature-a/function-a1
  • FeatureB函数:http://localhost:7071/api/feature-b/function-b1
  • FeatureC函数:http://localhost:7071/api/feature-c/function-c1

这样既实现了URL的功能分组,也完成了物理文件的归类。

3. 可选:用Proxies实现路由分组(无需修改函数路由)

如果不想逐个修改函数的function.json,可在项目根目录创建proxies.json文件,通过代理规则将分组路径映射到原函数:

{
  "$schema": "http://json.schemastore.org/proxies",
  "proxies": {
    "FeatureA-FunctionA1": {
      "matchCondition": {
        "route": "feature-a/function-a1"
      },
      "backendUri": "http://localhost:7071/api/FunctionA1"
    },
    "FeatureB-FunctionB1": {
      "matchCondition": {
        "route": "feature-b/function-b1"
      },
      "backendUri": "http://localhost:7071/api/FunctionB1"
    }
    // 其他函数的代理规则依次添加
  }
}

这种方式适合不想改动原有函数配置的场景,但物理文件仍在根目录,建议优先采用第一种方法,同时实现物理和URL的双重分组。

内容的提问来源于stack exchange,提问作者Branch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 15:35:20