Check out a free preview of the full Introduction to the JAMStack course
The "Setup API URL" 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 set up the API's endpoint using a redirect, and answers a question from the audience regarding database setup in the context of a serverless application.
Transcript from the "Setup API URL" Lesson
[00:00:00]
>> I don't really like this whole .netlify/functions thing, I want this to look more like a real API. So, I want my site to live at /api/hello, that's how I wanna call my functions. So, to do that, we can actually just add a redirect, and this uses some kinda funky TOML code, I think this is so that you can add multiple keys called redirects.
[00:00:24]
And, underneath redirects, we're gonna set from, and we want any call to API, so, API/anything, we want that to be redirected to .netlify functions, and then we want the value of this star, which apparently is called a splat. So, we set that to be the splat, and then we wanna send a status of 200 because this isn't a redirect, it's a rewrite.
[00:01:00]
And what the difference is that when you request a URL and you get sent to another URL, you'll get a HTTP status code of 301, and that means that there's been a permanent relocation of that asset. What we want here, is we want the /API/hello or whatever the function name is, we want that to be considered a permanent resource, we just wanted to call the Netlify special URL under the hood, so that it works.
[00:01:30]
So, we're sending it 200, so that the browser thinks that /API/hello, is a real permanent address. So, that's, we're just doing a URL rewrite, we're not actually like moving things around. So, upon saving this, we can run netlify dev again, and it'll continue to work at .netlify/function/hello. But now if I go in here and I write /API, we can still use it, but it looks a little more branded for our company.
[00:02:05]
You could also do this where you could sub deploy, a sub domain, so, api.yoursite.com/, and then you would just redirect everything to the functions folder, that's a perfectly valid use case as well.
>> So, I can see how you can set up services and front end serverless, as far as the database, are you thinking of exposing like a no SQL database, Dynamo or another flavor of
[00:02:34]
>> Yeah
>> NoSQL, so that you don't have to have a server, because if you have MySQL, you will have to have a server for a fully fledged site.
>> Right, so in the last section today, we will get into using serverless functions to call a database
>> Yeah
[00:02:50]
>> So, if you want to self host your database, yeah, you need to have that up and running. We'll be using a database as a service, which means that it's kind of off of our plate, and managed by someone else. And so an important distinction to draw when you're talking about the Jamstack, the Jamstack isn't eliminating complexity, it's making that complexity someone else's responsibility.
[00:03:14]
And what we're doing is we're effectively outsourcing that management responsibility to a specialized team. So, I am not a database expert, so managing and scaling a database is not something that I should be doing as my primary role, right? I'm a front end developer, I should be doing front end development.
[00:03:32]
So, by outsourcing to a managed database as a service company, the benefit that we get is that, that company presumably is hiring specialists at doing database management, who are going to do it better than I could or better than anyone on my team will presumably do, because they're front end developers, right?
[00:03:50]
We're not here to manage a database. So, yeah, the it's kind of that joke like the cloud is other people's computers, that's basically what Jamstack is to, it's not serverless, it's other people's servers. But we are by paying for, infrastructure as a service functions, as a service database as a service, the service that we're getting is that presumably these companies have hired experts to specialize in that one thing and doing it the best possible way, so that we can relax and know that they've got it at least as good as we would have.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops