Introduction to the JAMStack

Sending Data to Serverless Functions

Jason Lengstorf

Jason Lengstorf

Learn With Jason
Introduction to the JAMStack

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

The "Sending Data to Serverless Functions" 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 completes the application by adding functionality that fetches and posts data, and building an event handler.


Transcript from the "Sending Data to Serverless Functions" Lesson

>> We wanna get into the actual serverless function that's gonna send these form submissions. So the way that that will happen is we're going to get back into this functions folder over here, and we're gonna create a function called contact. So in our contact form, we can export the handler, and that is going to get the event.

And then we also need to get the callback, because we're going to send back something a little bit different than what we did before. And the way that we'll do it is we're gonna skip this context, cuz we don't need it, and the callback is the third argument.

So this is a full serverless function setup, is you'll get these three things. And the callback is a way to return that status code that's done wherever you want in your code, as opposed to being returned. And the reason that we're doing that is because the code that we're gonna use is callback-based.

And so instead of trying to unravel that, we're just gonna follow their conventions and use the callback. So the first thing that I wanna do is I wanna console.log the event so that we can see what comes back. And then we're going to send the callback, so the first argument to the callback is an error.

So if nothing goes wrong, we wanna pass null to signify that nothing is wrong. And the second thing is that object which has the status code, which we'll send as 200, and the body. And because we're gonna want to get JSON back, this is kind of a quirk of the way that these functions work, is they expect and they send and receive JSON, but they don't encode it for you.

So you end up having to do a lot of JSON.stringify. So let's just send an object that's boop: true. And this function, we want to be accessible from our Gatsby app, which means that we need to have a Gatsby app usable, ready to be used. But we need to run this in netlify dev.

So what's handy about this, whoops, is that Netlify will actually just auto-detect Gatsby if there is a Gatsby config file present. So we can just create an empty file called gatsby.config to tell netlify dev to use it. And now, if we run netlify dev, that localhost:8888 is gonna have our Gatsby site running, and we'll have access to our functions at the same time.

And if you remember, we already rewrote our functions to work at /api, which means that we'll be able to call our functions inside of our Gatsby app as fetch /api/thing. So we have this contact function here, and I want to just get this thing started, so let's run netlify dev.

And we can see that Netlify auto-detected Gatsby for us. So it also set up our Lambda server, so now everything's running, it's doing what we want. And if I go to 8888, we see our Gatsby app. But then if I go to /api/hello, our functions are still working, so that's what we want.

This is exactly what we want, because it means that now we can start interacting with our serverless functions as if it was on the live server. So let's go back into, let's close up some of these files, let's go back into our form. And let's make this form actually do something.

So I'm gonna just take all of this out, and what I wanna do instead is I'm gonna use that fetch API. And I'm gonna submit to /api/contact. And I wanna make the method POST, so that it doesn't add a query string or something. And I'm also going to set the body, so what we're gonna send to it is going to be a stringified version of our state.

What we get back from that is gonna be whatever we return from the contact function. So in this case, it'll be this stringified JSON. So using the fetch API, what we'll do is, we'll get our response. And then we want to convert that into JSON. And then we'll just get it, and we can do whatever we want with it.

So I'm going to console.log, That response, and then I'm also going to set the state to SUCCESS. And we'll catch, in case something goes wrong, an error. And if we get an error, we will console.error with whatever came back, and then we'll set the status to ERROR. Let me make this all much smaller so that we can see.

So this is our handler now, it is set up to actually submit an event and then respond and update the UI based on what comes back from that event. Does this make sense to everybody? So if I save this, I typo'd over here, status, not state, okay. So what we have now is the ability to send this form.

And it comes back from the function very quickly, because we don't have a lot of issues there. It's running locally, so there's not a lot of time, but we can see that it logged that boop: true. So our form is doing what we expected, it's responding appropriately. But what happens if it's not responding appropriately, what if we have an invalid response?

Let's try, so let's just send back a 500. So now, because I've sent back an error, what should happen is we should get a console.error logged, and it will give us the ERROR state. Okay, something went wrong, and this just straight-up exploded, didn't like it all. Maybe I need to make this into JSON, I don't care.

The errors themselves from the thing are formatted properly. But, so, yeah, that is a functioning form. We can actually use it. It's gonna do what we want.

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