当 JavaScript 和 TypeScript 文件位于同一文件夹中时,将 Azure Functions 从 V3 升级到 V4

问题描述 投票:0回答:1

当 JavaScript 和 TypeScript 文件都维护在同一文件夹结构中时,如何将 Azure Function 版本从 V3 升级到 V4?以下是 V3 模型的文件夹结构。 [azure function v3 模型中的文件夹结构]

<project_root>/
 | - .vscode/
 | - dist/
 | - node_modules/
 | - myFirstFunction/
 | | - index.ts
 | | - function.json
 | - mySecondFunction/
 | | - index.ts
 | | - function.json
 | - myThirdFunction/
 | | - index.js
 | | - function.json
 | - .funcignore
 | - host.json
 | - local.settings.json
 | - package.json
 | - tsconfig.json

此外,当 JavaScript 和 TypeScript 文件都存在于同一文件夹中时,如何为 Azure Function V4 模型指定 package.json 文件中 **main **key 的路径值?

“main”:“dist/src/functions/*.js”

尝试构建根文件夹,并将 typescript 文件转换为 JavaScript 并移动到 dist 文件夹,但 javascript 文件存储在同一文件夹中。生成生成后,如何运行 dist 文件夹中转换后的 JS 文件和根文件夹中的 jas 文件。

javascript typescript azure-functions
1个回答
0
投票

Node.js Azure Functions 的 Azure Functions V4 模型需要版本,请参阅MSDOC:

@azure/functions - package v4.*.*
Azure Functions Runtime - v4.25+
Azure Functions Core Tools - v4.*.*

主机.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  }
}

如果您不在函数代码中使用上下文,请将

(context, request)
切换为
(request, context)
或仅
(request)

Javascript:

const { app } = require('@azure/functions');

app.http('httpTrigger1', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    handler: async (request, context) => {
        context.log(`Http function processed request for url "${request.url}"`);

        const name = request.query.get('name') || await request.text() || 'world';

        return { body: `Hello, ${name}!` };
    }
});

打字稿:

import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";

export async function httpTrigger(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
    context.log(`Http function processed request for url "${request.url}"`);

    const name = request.query.get('name') || await request.text() || 'world';

    return { body: `Hello, ${name}!` };
};

app.http('httpTrigger', {
    methods: ['GET', 'POST'],
    authLevel: 'anonymous',
    handler: httpTrigger
});

.vscode/Settings.json:

{
    "azureFunctions.deploySubpath": ".",
    "azureFunctions.postDeployTask": "npm install (functions)",
    "azureFunctions.projectLanguage": "TypeScript",
    "azureFunctions.projectRuntime": "~4",
    "debug.internalConsoleOptions": "neverOpen",
    "azureFunctions.projectLanguageModel": 4,
    "azureFunctions.preDeployTask": "npm prune (functions)"
}
    V4 模型中的每个触发器不再需要
  • function.json
    文件,因为默认情况下在代码中配置绑定。
  • 不再需要应用程序设置
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
    即可使 v4 模型功能正常工作。

如何运行 dist 文件夹中转换后的 JS 文件和根文件夹中的 jas 文件。

模型根据

main
中提供的
package.json
字段加载函数。您必须使用
main
字段提及 JavaScript 或 Typescript 函数的入口点。

Javascript:

src/index.js
src/functions/*.js
src/{index.js,functions/*.js}

打字稿:

dist/src/index.js
dist/src/functions/*.js
dist/src/{index.js,functions/*.js}

我有一个包含 Javascript 和 Typescript Azure 函数的函数项目。

Package.json:

{
  "name": "",
  "version": "1.0.0",
  "description": "",
  "main": "dist/src/functions/*.js",
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "clean": "rimraf dist",
    "prestart": "npm run clean && npm run build",
    "start": "func start",
    "test": "echo \"No tests yet...\""
  },
  "dependencies": {
    "@azure/functions": "^4.0.0"
  },
  "devDependencies": {
    "azure-functions-core-tools": "^4.x",
    "@types/node": "18.x",
    "typescript": "^4.0.0",
    "rimraf": "^5.0.0"
  }
}

模型加载了打字稿函数,因为我已配置 main 字段来加载

dist
文件夹中存在的函数。

[2024-11-14T09:40:04.469Z] Worker process started and initialized.

Functions:

        httpTrigger1: [GET,POST] http://localhost:7071/api/httpTrigger1

        httpTrigger2: [GET,POST] http://localhost:7071/api/httpTrigger2

For detailed output, run func with --verbose flag.
[2024-11-14T09:40:09.393Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.

main
字段更新为
"src/functions/*.js"
中的
Package.json
以运行 JavaScript 函数。

[2024-11-14T10:01:14.867Z] Worker process started and initialized.

Functions:

        httpTrigger: [GET,POST] http://localhost:7071/api/httpTrigger

For detailed output, run func with --verbose flag.
[2024-11-14T10:01:27.902Z] Executing 'Functions.httpTrigger' (Reason='This function was programmatically called via the host APIs.', Id=478aa6d6-00b3-400d-a328-abefd658e4fe)
© www.soinside.com 2019 - 2024. All rights reserved.