An API that’s considered to be RESTful should be stateless, use HTTP verbs, and expose a directory-like URL pattern for routes. While REST is not a standard, it’s how most of the modern web APIs are built.
- [Scott] So now, we're going to be getting RESTful. Does anybody want to give me their definition of REST? Not what the acronym stands for but what it actually means? Don't the read mine. Here there's a ton of people trying to get. Yo. You had your hand up? - [Student] Oh, no-- - Oh I thought you were raising your-- I was just stretching. I wasn't really raising my hand, although you were like this. (students laugh) Anybody? I know people have some meaning of REST. There's no wrong answer. Because REST is like kind of...
I mean there's like a 50 page definition on it. So I think whatever you're thinking of is probably right. So I just want to know what you think about it. Nobody? Alright, so REST. REST is not a framework, like you can't just npm install REST. Actually you know what, you probably could npm install REST. I'll take that back. But it wouldn't be what you think it is. It would be something else. Just nobody, I mean, the stuff that's on npm these days.
But yeah, it's not a framework. And it's not a standard. It's not like, it's not like an HTTP protocol. It's not like something that's baked-in. W3C would never talk about REST. Like it's not a standard in the web. It's just a way that we came up with. So the modern web is mostly built around REST. As far as the acronym, it's just short for Representational State Transfer. I think that's what it's short for. What does that actually mean, we'll get into that. But the basics are is that it should be stateless.
What I mean by statless, I mean when I access a web service, like a server or some other client or whatever. When I make a request, that request should be stateless, as in it shouldn't pass around information about previous requests and stuff like that. It should be asking or doing a request that's entirely fresh and entirely stateless from the previous one or the next one. So completely stateless. Use HTTP verbs explicitly. Express uses the verbs for everything as you saw, app.get, app.post.
Those are the HTTP verbs. REST uses those verbs explicitly. And then expose a directory-like URL pattern for our routes. So what I mean by that, if we go look at, well this is actually a really bad example on this one but, when I say directory structure. It'll make more sense as we start building the URL, but the routes for our requests should follow kind of like a directory structure leading to a resource. And you'll see what I mean in a little bit. That's horrible spelling.
And also, it should transfer JSON or XML. Mostly JSON nowadays, but XML is still around. So that's usually, when I think about REST, that's what I think about, those four things. Stateless, using the verbs, directory-like URL pattern for the routes, and emits and consumes JSON and or XML. That's it. There are some smaller details that I encourage you to read up on if you want to get like really get with it. But I would say Rails has probably made REST very popular but REST is definitely the thing.
Compared to other things like SOAP, and other standards out there. REST is as a thing, it can do the same thing as those other different types of paradigms. So there are a ton of tools out there to assist with designing of your RESTful API. I mean there are startups whose job is to give you tools to help you design and document an API. And then even those tools will go as far as grabbing your API documentation, and creating an API server for you. Just for marks, not for production. So it's very important, like it can be very tough modeling an API.
So there are tools out there that help you with that. We will not be using those tools. One, our API's anatomy are complex. And two, you still need to learn how to design an API yourself. And get going with it. So we won't be using some of those tools. But they do exist if you get to the point where you want to abstract some of the basic stuff away. And you get a good feeling what REST is.
This course was created by Frontend Masters. It was originally released on 12/30/2015. We're pleased to host this training in our library.
- Executing Node.js
- Using Express
- What is middleware?
- Testing in Node.js
- Using Mongo with Node.js
- Data modeling
- Querying data with Mongoose
- Identifying sensitive routes
- Configuring the deployment