Search code examples
node.jstypescriptexpressazure-functions

How to connect express js from azure function Model v4?


My azure function using Model v4

index.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);

app.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;

my express app

I'm trying to execute express js APIs from azure function , seems i got the response from azure function like 'Hello' but I expected the response from express like "Hello from Express!".


Solution

  • azure-aws-serverless-express works perfectly fine for both Javascript and Typescript V3 model Azure function.

    Typescript V3 function-

    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"
    }
    
    • I am able to get the expected response.

    enter image description here

    enter image description here

    • Where as for V4 model, it keeps complaining about The "url" argument must be of type string. Received undefined after passing the Url in string format. This error is referring to the package folder.

    enter image description here

    • AFAIK, azure-aws-serverless-express is not yet compatible with V4 models.