Introduction to Serverless Functions

Creating a Serverless Function

Jason Lengstorf

Jason Lengstorf

Learn With Jason
Introduction to Serverless Functions

Check out a free preview of the full Introduction to Serverless Functions course

The "Creating a Serverless Function" Lesson is part of the full, Introduction to Serverless Functions course featured in this preview video. Here's what you'd learn in this lesson:

Jason demonstrates how configure the initial settings of a new file within another TOML file, and how to build a simple serverless function.

Preview
Close

Transcript from the "Creating a Serverless Function" Lesson

[00:00:00]
>> I want to dig into how to actually build our first serverless function. So, let's get this thing configured. So what we're gonna do is let's open up this netlify.toml. And we're gonna write the configuration that we need to get serverless functions to run. So I'm gonna add on line 4 here, I'm gonna write functions = functions.

[00:00:22]
And so what we're telling Netlify is that the serverless functions for this repo, for this project are gonna be served from a folder called functions. So once I've done that, I'm gonna come out here and I'm gonna create a folder called functions. And I can save this, and this looks like it redirected.

[00:00:43]
We are gonna have to stop and restart it to get it to run. So let's go ahead and stop the server now. And then let's create our first serverless function. So I'm gonna go into this functions folder, I'm gonna create a new file. And I'm just gonna name it boop.js.

[00:00:59]
Now I can pull this over to the side so that you can see. This is our config, we just added this functions = functions. This is written in TOML. If you're unfamiliar with TOML, TOML is tricky. It's very human readable and tell it's not. Fortunately, this is all of the TOML we're gonna write today.

[00:01:24]
So, if you get deeper into it you'll you may understand why I hesitate to be like TOML was great. But for fortunately today, we're not gonna have to write any TOML other than just telling it where to find functions. Inside this function boop.js, so /functions/boop.js, we are going to create our first serverless function.

[00:01:46]
And the way that we do this is we need to export a constant called handler. And we're using I think this is common js is the syntax. And we're gonna add something to the exports object. So exports.handler, and that's going to equal our serverless function. And so what we've just done here, is we have said, this file, export something called handler.

[00:02:19]
So the way that serverless functions work is that we need to tell the serverless function system, how to deal with incoming requests. And it does that by calling a function called handler. So we give it this handler. And then the reason I made it async is because I don't wanna have to fire a callback.

[00:02:38]
I just wanna be able to return a value. And so we can do that by returning an object. And our object has two properties. A status code, this is gonna be any HTTP status code. We want it to be okay, which is 200. And then we need to give it some kind of a body.

[00:03:01]
In this, believe it or not, is your first serverless function. This is completely functional, it works. It does what we expect, and it shows you the bare minimum requirements. And this is all that's required, if you want to do serverless functions. We have a handler, that handler returns a status code in a body.

[00:03:22]
And that's it, right? So let's save this. And then I'm gonna start that server again with netlify dev. And I wanna show you that now it shows me here that the function server is listening. And it's also running the eleventy site. So it's running everything. And so we can see that like eleventy is running independently here.

[00:03:49]
The functions are running on a different port, but all of that gets bundled up into this local host 8080. And this is the way that that Netlify works. And this is one of the reasons that I think it's such a powerful model.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now