如何从azure函数模型v4连接express js?

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

我使用模型 v4 的 azure 函数

索引.ts

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

app.http("httpTrigger1", {
  methods: ["GET"],
  route: "api/{*segments}",
  handler: async (context, request) => {
    console.log("context ::::: ", context);
    console.log("request :::::: ", request);

    return { body: `Hello` };
  },
});

module.exports = azureFunctionHandler(expressApp);

应用程序.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;

我的快递应用程序

我正在尝试从azure函数执行express js API,似乎我收到了azure函数的响应,例如“Hello”,但我期望来自express的响应,例如“Hello from Express!”。

node.js typescript express azure-functions
1个回答
0
投票

azure-aws-serverless-express
对于 Javascript 和 Typescript V3 模型 Azure 函数来说都完美无缺。

Typescript V3 功能-

index.ts-

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

const httpTrigger: AzureFunction = function (context: Context, req: HttpRequest) {
     azureFunctionHandler(expressApp)(context, req);
};

export default httpTrigger;

app.ts-

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

app.get('/api/user', (req,res) => res.send("Hello from Express!"));

export default app;

function.json-

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ],
      "route": "{*segments}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "scriptFile": "../dist/HttpTrigger1/index.js"
}
  • 我能够得到预期的回应。

enter image description here

enter image description here

  • 对于V4模型,在以字符串格式传递Url后,它不断抱怨
    The "url" argument must be of type string. Received undefined
    。此错误涉及包文件夹。

enter image description here

  • AFAIK,azure-aws-serverless-express 尚未与 V4 模型兼容。
© www.soinside.com 2019 - 2024. All rights reserved.