Skip to main content
AI
  • Get started with AI

    • Red Hat AI

      Accelerate the development and deployment of enterprise AI solutions.

    • AI learning hub

      Explore learning materials and tools, organized by task.

    • AI interactive demos

      Click through scenarios with Red Hat AI, including training LLMs and more.

    • AI/ML learning paths

      Expand your Red Hat OpenShift AI knowledge using these learning resources.

    • AI quickstarts

      Focused AI use cases designed for fast deployment on Red Hat AI platforms.

    • No-cost AI training

      Foundational Red Hat AI training.

  • Featured resources

    • Red Hat OpenShift AI
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

Red Hat AI Factory with NVIDIA

Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.

Learn
  • Self-guided

    • Documentation

      Find answers, get step-by-step guidance, and learn how to use Red Hat products.

    • Learning paths

      Explore curated walkthroughs for common development tasks.

    • Guided learning

      Receive custom learning paths powered by our AI assistant.

    • See all learning
  • Hands-on

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

    • Interactive labs

      Learn by doing in these hands-on, browser-based experiences.

    • Interactive demos

      Click through product features in these guided tours.

  • Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics
  • Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
Build
  • Get started

    • Red Hat build of Podman Desktop

      A downloadable, local development hub to experiment with our products and builds.

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

  • Download products

    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products
  • Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset
  • References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
Community
  • Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions
  • Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github
  • Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

Join the Red Hat Developer program

Download Red Hat products and project builds, access support documentation, learning content, and more.

ConsoleDocsSupport Search
New For you

Recommended

We'll recommend resources you may like as you browse. Try these suggestions for now.

  • Product downloads
  • Cheat sheets
  • Red Hat Developer learning portal
  • Developer blog
  • E-books
  • Developer Sandbox
Log in

Get more with a Red Hat Developer account

  • Product downloads
  • Console access
  • Event registration
  • Training & trials
Explore more account benefitsLog in or register

A subscription may be required for some services.

Contact us

Node.js serverless functions on Red Hat OpenShift, Part 2: Debugging locally

July 13, 2021
Lucas Holmquist
Related topics:
KubernetesNode.jsServerless
Related products:
Red Hat OpenShift

    Welcome back to our series on using serverless functions on Red Hat OpenShift. The previous article introduced you to how logging works in Node.js and how to customize what is logged in a Node.js function application. Now, we'll take a look at how to debug Node.js function-based applications. Because debugging is a longer topic, we'll cover it in two parts. This article walks through how to set up and debug function applications locally with Visual Studio Code (VS Code). The next article will show you how to connect and debug function applications running in a container on a cluster.

    Note: For an introduction to logging function-based applications, see Node.js serverless functions on Red Hat OpenShift, Part 1: Logging. For an overview of Red Hat OpenShift Serverless Functions, see Create your first serverless function with Red Hat OpenShift Serverless Functions.

    Prerequisites

    To follow along with this article, you will need to install Node.js and download the example application from GitHub. We'll also use VS Code for its easy-to-use built-in debugger.

    As with the previous article, we scaffolded this function application with the kn func command-line interface (CLI) tool. If you are not already familiar with it, you can learn more by reading Create your first serverless function with Red Hat OpenShift Serverless Functions.

    Setting up the function application in Visual Studio Code

    Use Git to clone the example repository and then open it up in VS Code. We can see that this Node.js function application is just like any other Node.js application, with an index.js file where the main function logic is located.

    Before we continue, let's put a breakpoint right around line 30, which is inside the invoke function (see Figure 1).

    Adding a breakpoint to a serverless function in VS Code.
    Figure 1: Adding a breakpoint to a serverless function in VS Code.

    We are setting the breakpoint here because we want to be able to halt the execution of the function when it is called, and the invoke function is the entry point generated by the kn func CLI tool. This allows us to step through the code and inspect the different variables the function provides as the function executes.

    Let's take a look at the package.json file. We can see in the following code example that there are three npm scripts generated by the kn func CLI tool: one for running, another for testing, and another one for debugging. This last script is the one we are interested in.

    "scripts": {
    
        "test": "node test/unit.js && node test/integration.js",
    
        "local": "faas-js-runtime ./index.js",
    
        "debug": "nodemon --inspect ./node_modules/faas-js-runtime/bin/cli.js ./index.js"
    
      }

    There are a few things to note about this debug script. First, it uses Nodemon to start the Node.js process. Nodemon will also detect any code changes and restart the Node.js process when the changes are saved.

    The second is the --inspect flag. This allows us to stop the Node.js process at any breakpoints we set. At the moment, we've only set one.

    The last is that the script is called with the faas-js-runtime CLI. This is a module that provides a Node.js framework for executing a function. The function listens for incoming HTTP requests at localhost:8080. The incoming request can be a CloudEvent or just a simple HTTP GET request. To learn more about the faas-js-runtime, check out the project on GitHub.

    Debugging the function application

    Starting the debugging process is fairly simple. Select Start Debugging from the Run menu, as shown in Figure 2.

    Screenshot showing the debug menu in VS Code.
    Figure 2: Starting the debug process in VS Code.

    This initializes the Node.js process using the --inspect flag and Nodemon. Once the process starts, your function runs at http://localhost:8080. Navigating to this URL should activate the breakpoint that we set earlier (see Figure 3).

    breakpoint active
    Figure 3: Activating the breakpoint in VS Code.

    From here, we can inspect any of the variables that are available to us. Functions are invoked with a context object, which can be inspected easily using VS Code's variable inspector on the left-hand side of the interface (as shown in Figure 4). This object provides access to the incoming request information. You can get the HTTP request method, any query strings sent with the request, the headers, the HTTP version, or the request body. If the incoming request is a CloudEvent, the CloudEvent itself will also be found on the context object.

    Context object expanded in debug
    Figure 4: Context object expanded in debug mode.

    The request is a simple GET request. We can see from the variable inspector that it has no body or query params. As with most debugging tools, you can perform many debugging functions like stepping into and over a method, as well as just telling the process to continue executing.

    Next, let's send a request to the function with a body. You can use this curl command:

    curl -X POST -d '{"hello": "world"}' -H'Content-type: application/json' http://localhost:8080

    When the process stops this time, we can see that there is some data in the context.body:

    {
      context: {
        body: {
          hello: “name” 
        }
      }
    }

    If the request was sent as a CloudEvent, this will help you easily inspect the request headers to learn more about it:

    curl -X POST -d '{"hello": "world"}' \
      -H'Content-type: application/json' \
      -H'Ce-id: 1' \
      -H'Ce-source: cloud-event-example' \
      -H'Ce-type: dev.knative.example' \
      -H'Ce-specversion: 0.2' \
      http://localhost:8080

    To learn more about this context object and the parameters it provides to the function developer, check here. To learn more about CloudEvents, check here.

    Conclusion

    This article introduced you to debugging a Node.js serverless function application locally while you develop the function application. Stay tuned for the next part of this series, where we'll look at how to debug the function application while running inside a container on a Kubernetes cluster such as Red Hat OpenShift.

    While you wait, you can read about the latest on OpenShift Serverless Functions. To learn more about what Red Hat is up to on the Node.js front, check out our Node.js topic page.

    Last updated: September 19, 2023

    Related Posts

    • Create your first serverless function with Red Hat OpenShift Serverless Functions

    • Node.js serverless functions on Red Hat OpenShift, Part 1: Logging

    • What's happening in the Node.js community

    Recent Posts

    • What's new for developers in Red Hat Enterprise Linux 10.3 and 9.9

    • Control multicluster costs with right-sizing in Red Hat Advanced Cluster Management 5

    • Lightbits is now certified for Red Hat OpenStack Services on OpenShift

    • What's new in Red Hat OpenShift 5 for developers

    • On-demand screen context in Red Hat Developer Hub intelligent assistant

    Red Hat Developers logo

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    Red Hat Developer

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility
    Ask AI