是否有任何模块可以从azure V4函数执行express api而不是模块“azure-aws-serverless-express”?

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

当我从 azure 函数模型 V4 调用执行时,获取请求 url 为未定义,

auzurev4\src 函数\httpTrigger1.ts

import { InvocationContext, HttpRequest } from "@azure/functions";
import { app } from "@azure/functions";
import azureFunctionHandler from "azure-aws-serverless-express";
import expressApp from "../../app";

const httpTrigger: any = async function (
  context: InvocationContext,
  req: HttpRequest
): Promise<void> {
  await azureFunctionHandler(expressApp)(context, req);
};

export default app.http("httpTrigger1", {
  methods: ["GET"],
  route: "{*segments}",
  handler: httpTrigger,
});

auzurev4 pp.ts

import express, { Request, Response } from "express";
const app = express();

app.get("/api/user", (req: Request, res: Response) => {
  res.send("Hello from Express!");
});

export default app;

尝试 api url

http://localhost:7071/api/user

文件夹结构

https://imgur.com/a/CCn4j5X

我使用 azure-aws-serverless-express 成功从 auzure v3 获得了 api 响应,但在执行 azure v4 时出现此错误。所以请指导我解决这个问题或者请分享任何其他替代模块

错误:

> [email protected] start
> func start


Azure Functions Core Tools
Core Tools Version:       4.0.5571 Commit hash: N/A +9a5b604f0b846df7de3eb37b423a9eba8baa1152 (64-bit)
Function Runtime Version: 4.30.0.22097

[2024-04-01T11:51:19.207Z] Worker process started and initialized.

Functions:

        httpTrigger1: [GET] http://localhost:7071/api/{*segments}

For detailed output, run func with --verbose flag.
[2024-04-01T11:51:24.152Z] Host lock lease acquired by instance ID '0000000000000000000000000E007AAF'.
[2024-04-01T11:51:24.471Z] Executing 'Functions.httpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=a44e015d-16ec-4c2b-b90d-31b04740c1c7)
[2024-04-01T11:51:24.568Z] Executed 'Functions.httpTrigger1' (Failed, Id=a44e015d-16ec-4c2b-b90d-31b04740c1c7, Duration=116ms)
[2024-04-01T11:51:24.570Z] System.Private.CoreLib: Exception while executing function: Functions.httpTrigger1. System.Private.CoreLib: Result: Failure
Exception: The "url" argument must be of type string. Received undefined
Stack: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received undefined
    at validateString (node:internal/validators:162:11)
    at Url.parse (node:url:170:3)
    at Object.urlParse [as parse] (node:url:141:13)
    at D:\mugesh\auzurev4\node_modules\azure-aws-serverless-express\dist\index.js:26:29
    at D:\mugesh\auzurev4\dist\src\functions\httpTrigger1.js:24:73
    at Generator.next (<anonymous>)
    at D:\mugesh\auzurev4\dist\src\functions\httpTrigger1.js:12:71
    at new Promise (<anonymous>)
    at __awaiter (D:\mugesh\auzurev4\dist\src\functions\httpTrigger1.js:8:12)
    at httpTrigger (D:\mugesh\auzurev4\dist\src\functions\httpTrigger1.js:23:12).
javascript node.js azure express azure-functions
1个回答
0
投票

您可以直接使用以下代码,而不是使用

azure-aws-serverless-express
:-

httpTrigger1.ts:-

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

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

    try {
        const response = await appInstance.get("/api/user")(request, null);

        return {
            body: response.body
        };
    } catch (error) {
        return {
            status: 500,
            body: JSON.stringify({ message: "Error calling Express API", error: error.message })
        };
    }
};

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

app.ts:-

import express, { Request, Response } from "express";

const app = express();

export function createUserHandler() {
    return (req: Request, res: Response) => {
        res.send("Hello from Express!");
    };
}
export default app;

输出:-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.