The serverless function is similar to executing the Node.js function whenever HTTP requests hit a respective API route.
Javascript is the client-side language that runs on a browser. JavaScript is not able to perform actions related to computerized things like reading the file, handling HTTP requests, connecting to the database, etc. We can use Node.js to perform computerized stuff. Node Js is the JavaScript runtime environment that allows you to run javascript on the server.
In the react component, we use JSX as a templating language. JSX allows you to write HTML-like syntax in your JavaScript code.
In the JSX syntax, We can't write backend code (serverless function). We can choose the following options for writing serverless functions:
We choose the second option for writing the serverless function as the Gatsby functions are built-in into the Gatsby framework. You can read more about the Gatsby function from the below page.
Many of the companies e.g. AWS, Netlify, GCP, etc provide a platform where can write serverless functions, and you are charged based on their plan and usage. But... running a serverless function on 3rd party server will require installing 3rd party package or spinning up the dev server.