Check out a free preview of the full Introduction to the JAMStack course

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

Jason demonstrates how to write a function that uses a handler, and how to build a toml file that communicates with Netlify and informs Netlify about the location of the serverless functions. A serverless function can be programmed to exclusively communicate with one specific domain using a hidden key.

Preview
Close

Transcript from the "Create & Run a Serverless Function" Lesson

[00:00:00]
>> So those function is you typically have a function called handler. And we're using the node style module syntax here. So this is a named export called handler and I like to use the async approach here. Rather than having to do callback juggling or things, we will look at cases where the callback is the right way to do it.

[00:00:29]
But then we just need to return an object. And we need to status code this is going to be an HTTP status, 200 means okay. So that's kind of your that's what you're shooting for usually is like, hey, this worked. And then we want to send something back so the body is going to be hello world.

[00:00:50]
Okay, so we have now defined our first serverless function. How do we make it go? The way that we can do this is you can use a framework like serverless, which has local support. We've already got the Netlify CLI installed, so we're gonna use that. And to make that work, we just have to signify a couple things to Netlify.

[00:01:13]
So I'm gonna create a netlify.toml. TOML is the more confusing version of YAML. [LAUGH] And what we're gonna do is we're gonna tell netlify using the build key where the functions are. And that's gonna be in the functions directory. And then that is actually all we have to do.

[00:01:37]
So let's run netlify dev, so netlify dev is a way that you can test things out locally. And when you're dealing with serverless functions, this is really, handy. Because otherwise, you'd have to deploy it to see what changed in that, its loops, right? And you want to reduce the loops.

[00:01:54]
You want to be able to get feedback very, very quickly. So I'm going to run notify dev and we can see it says that the Lambda server is listening. And this is the actual URL that things are available at, port 8888. So let's open that up. Okay, nothing happened yet.

[00:02:18]
But that's because functions live and netlify at a special location, which is .netlify/functions and then the name of of your functions. So in our case its hello, and so when I enter this, we can see that we got hello world and then if I wanna make some changes I'm able to see those live without having to restart, or anything like that we get live feedback.

[00:02:50]
So this is at its core the simplest possible iteration of a serverless function. And we got this up in what? Six lines of code. So I find this to be a really, really approachable way to start building backend functionality. So if we needed to do something like make an authenticated request, where we need a server key, which we'll actually look at in a minute.

[00:03:15]
This is a great way to obscure that. So, one of the big challenges of the jam stack is that you will be making requests that often use API keys. And if that API key is privileged, you can't put that on your client-side app. Because if you put a client, a privileged key into frontend code, that key becomes part of the asset that gets generated.

[00:03:39]
It becomes available to anybody who's poking around in your source code. So naturally, you shouldn't ever do that because that means that somebody could find that key and then do nefarious things with it. So to work around that problem, what you can do instead, is you can call a serverless function.

[00:03:55]
And that serverless function can be set up to only receive requests from your domain and then that serverless function has the key abstracted away and hidden, nobody can find it. And that way you can make those privileged calls without having to actually do without putting that key in public.

[00:04:13]
So these are some kind of nice uses of how to make this work.

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