API Design in Node.js (using Express & Mongo) Getting RESTful
This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Getting RESTful" Lesson
>> Speaker 1: So now, we're gonna be Getting RESTful. Does anybody wanna give me their definition of rest? Not what the acronym stands for, but what it actually means? Don't read mine. [LAUGH] He was trying to read it. Yo, you got your hand up?
>> Speaker 2: No, I was just-
>> Speaker 1: I though you were raising, you're just.
[00:00:21] I was just stretching, I wasn't really raising my hand, although you were like this. Anybody? I know people have some meaning of rest. There's no wrong answer cuz rest is kind of, I mean, there's a 50 page definition on it, so I think, whatever you're thinking of is probably right.
[00:00:41] So I just wanna know what you think about it.
>> Speaker 1: Nobody? All right, so REST is not a You can't just NPM install REST. Actually, you know what, you probably could NPM install REST. [COUGH] I'll take that back.
>> Speaker 1: [COUGH] But it wouldn't be what you think it is.
[00:01:04] It'd be something else. The stuff that's on NPM these days. But yeah, it's not a framework. And it's not [COUGH] a standard. It's not It's not like a HTTP protocol, it's not like something that's baked in. W3C will never talk about REST, like it's not a standard in the web, it's just a way that we came up with.
[00:01:23] So the modern web is mostly built around Rest, and 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 stateless, I mean when I access web service, like a server or some other client or whatever, when I make a request, that request should be stateless.
[00:01:49] As in it shouldn't pass around information about previous requests and stuff like that. It should be asking, or it should be 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. So, express uses the verbs for everything as you saw, at dot get, at dot post, those are the HTTP verbs.
[00:02:15] REST uses those verbs explicitly. And then they expose a directory-like URL pattern for our routes. So, what I mean by that, if we go look at, actually this is 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.
[00:02:40] And you'll see what that means in a little bit. That's horrible spelling. And also, it's a transfer of JSON or an XML. Mostly JSON in nowadays age 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.
[00:03:04] That's it. There are some smaller details that I encourage you to read up on if you wanna get really good with it, but yeah, I would say Rails probably make RESTful very popular. But REST is definitely the thing. Comparing it to other things like soap and other standards out there, REST is the thing and can do the same think as those other different types of paradigms.
[00:03:30] So there are a ton of tools out there to assist with designing of your RESTful API. 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 mocks not for production.
[00:03:49] So it's very important, it's very, 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. For one, our APIs are not gonna be that complex. And two, you still need to learn how to design API yourself and get going with this.
[00:04:04] 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.