Error 404 Not Found on all routes Azure function app Node TypeScript

Vlad Makar 0 Reputation points
2024-04-15T11:47:48.4966667+00:00

Hello everyone. I I am getting an error 404 Not Found on all routes on request to mine api. I can't figure out how to write the code correctly. Here example of my code:

host.json:

{
    "version": "2.0",
    "extensions": {
        "http": {
            "routePrefix": ""
        }
    },
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[4.*, 5.0.0)"
    }
}

local.settings.json:


{
    "IsEncrypted": false,
    "Values": {
      "AzureWebJobsStorage": "",
      "FUNCTIONS_WORKER_RUNTIME": "node",
      "AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
    }
  }

index.ts(I tried different methods to write this):


import { app } from "@azure/functions";

import { helloController } from "./controllers/hello.controller";

app.http("hello", {
  methods: ["GET"],
  authLevel: "anonymous",
  route: "hello",
  handler: helloController.hello.bind(helloController),
});

helloController:


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

class HelloController {
  public async hello(req: Request, res: Response, next: NextFunction) {
    try {
      return res.sendStatus(200).json("Hello World");
    } catch (e) {
      next(e);
    }
  }
}

export const helloController = new HelloController();

github.yml:


name: Build and deploy Node.js project to Azure Function App - buxonline

on:
  push:
    branches:
      - node_version
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: 'backend' # set this to the path to your web app project, defaults to the repository root
  NODE_VERSION: '20.x' # set this to the node version to use (supports 8.x, 10.x, 12.x)

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout GitHub Action'
        uses: actions/checkout@v4

      - name: Setup Node ${{ env.NODE_VERSION }} Environment
        uses: actions/setup-node@v3
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: 'Resolve Project Dependencies Using Npm'
        shell: bash
        run: |
          pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
          npm install
          npm run build --if-present
          npm run test --if-present
          popd

      - name: Zip artifact for deployment
        run: zip release.zip ./* -r

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3
        with:
          name: node-app
          path: release.zip

  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    permissions:
      id-token: write #This is required for requesting the JWT

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: node-app

      - name: Unzip artifact for deployment
        run: unzip release.zip

      - name: Login to Azure
        uses: azure/login@v1
        with:
          client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_689801BD1C5E47F3B0BE98C1A283FD1E }}
          tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_5D60349DAA2042FEB1925BC25A5A1309 }}
          subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_DB7120F777A048AFAF9B033F5A591B7A }}

      - name: 'Run Azure Functions Action'
        uses: Azure/functions-action@v1
        id: fa
        with:
          app-name: 'buxonline'
          slot-name: 'Production'
          package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}

Structure of my project:
-project-root:

  • .github
    • workflows
      • nameofmyyml.yml
  • backend
    • .env
    • .eslintrc.js
    • host.json
    • local.settings.json
    • package.json
    • tsconfig.json
    • src
      • config
      • constants
      • controllers
      • enum
      • error
      • middleware
      • models
      • routers
      • services
      • statics
      • types
      • app.ts,
      • index.ts
  • dist
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,281 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 25,821 Reputation points Microsoft Employee
    2024-04-15T15:44:58.5566667+00:00

    Hi @Vlad Makar

    By default, function registration is set to dist/src/functions/*.js. With moving your entry point to src/index.js, the worker doesn't know where to find to your function(s). This can be changed in package.json by setting "main": "dist/src/index.js".

    See Node.js developer reference for Azure Functions | Microsoft Learn for more examples.