Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's put the concepts we've learned into practice by building a very simple REST API!
Nodemon has been included as a dependancy in the project files, but see this resource for installing nodemon globally.
-
0:00
Remember, unlike a traditional server-side application, which responds to requests
-
0:05
with HTML, a REST-ful API responds with data, most of the time a JSON object.
-
0:09
To see how this works,
-
0:11
let's write a simple route that responds to a GET request with JSON.
-
0:15
To follow along with me, download the starter files from the link on this page.
-
0:20
Locate the project folder from the terminal and
-
0:22
run npm install, to install the project's dependencies.
-
0:26
Then open the starter files in your preferred text editor.
-
0:29
We're starting off with some standard files for a basic Express app.
-
0:33
Along with a file called records.js, and a file called data.json,
-
0:37
both of which we'll discuss very soon.
-
0:39
Right now, let's look at the app.js file,
-
0:42
which is where we'll begin writing our code.
-
0:44
Open up that file, and you'll see that we're importing express,
-
0:48
creating an express app, and listening on port 3000.
-
0:51
We'll begin by creating an Express GET route handler.
-
0:55
Recall that the Express function adds a bunch of methods to Node's HTTP server,
-
1:00
to make it easier for us to receive and respond to requests.
-
1:04
These Express methods include GET, POST, PUT, and DELETE.
-
1:08
We're creating a GET method.
-
1:12
The GET method takes two arguments.
-
1:14
The first argument is the route that we wanna handle.
-
1:16
Let's write some code that will respond to a request to a route called greetings.
-
1:20
So in quotes, we'll say '/greetings'.
-
1:23
The second argument is the callback function we want to run when this request
-
1:28
comes in, how we want to respond.
-
1:33
Recall that when a client, a browser, or some other application makes a get request
-
1:37
to this URL, Express will run the code inside of this callback function.
-
1:43
This callback function takes at least two arguments,
-
1:46
one representing the incoming request from the client, and
-
1:49
one representing the outgoing response from the server.
-
1:52
By convention, they're both typically shortened to req for
-
1:57
request, and res for response.
-
1:59
Recall that in a traditional server-side Express application,
-
2:03
you'd respond to a client request using the Express res.render function,
-
2:08
to render a template and send HTML back to the client.
-
2:11
So you might do something like this.
-
2:13
You'd say res.render('some_html_template').
-
2:19
But we don't wanna do that, because we're building a REST API.
-
2:22
So inside the callback function, we wanna send back JSON.
-
2:25
So to do that, we can use the JSON method available on the response object.
-
2:30
So we can say res.json.
-
2:33
And we can pass the JSON method an object, and
-
2:36
it will convert that object to JSON and send it to the client.
-
2:41
So let's pass the JSON method an object.
-
2:45
And this object can be a greeting, Hello World!
-
2:49
Now let's save and test this out.
-
2:52
Open up a terminal on your computer.
-
2:54
Or if you're using VS Code, open your integrated terminal by
-
2:58
selecting it from the menu or using the keyboard shortcut Ctrl+\.
-
3:03
Type npm start, to get the server running.
-
3:07
Recall that this project is using nodemon to run the server, so
-
3:10
we don't have to keep restarting.
-
3:12
See the teacher's notes if you need assistance installing nodemon.
-
3:16
As you can see from the message in the console,
-
3:18
our app should be running at local host 3000.
-
3:21
If we visit localhost:3000/greetings from a browser,
-
3:28
You can see that our API is sending JSON when we make a request.
-
3:32
The browser, by default, sends a GET request.
-
3:35
By visiting localhost:3000/greetings,
-
3:38
we're sending a GET request to the greetings route, which we've programmed in
-
3:43
our Express application to send back a JSON object containing a greeting.
-
3:47
You've written code that responds to JSON when a client makes
-
3:50
a request to an endpoint.
-
3:51
At a high level, that's really all there is to it.
-
3:54
You've written a very simple REST API.
-
3:56
If all you want is a REST API that returns a simple message, then congratulations,
-
4:01
you're done.
-
4:02
But we want a REST API that provides much more functionality.
-
4:05
We want to display, create, update, and delete quotes.
-
4:08
With this in mind, let's plan out our application and
-
4:11
start building our first routes.
You need to sign up for Treehouse in order to download course files.
Sign up