All right.
So now that we're ready to create are fully fledged RESTful API let's revisit this table that we saw
previously where we have the HTTP verbs on the left those five trusty verbs: get, post, put, patch
and delete. And then we've got our RESTful routing over here. One which is for the overall category for
all of the articles that give me access through our API and then one which is for a specific resource
inside that collection.
So in this case we're using an article on Jack Bauer as the example.
Now in order to make our API RESTful, we're going to go through this table and create each of these routes
in turn and we're going to build it up from scratch.
And the first thing we're going to tackle is of course our get route which fetches all of the articles
using the /articles route. So we can do this using Express by writing app.get specifying
our route and then adding a callback function which can tackle the request and response.
And we've seen this and done this a few times now so it shouldn't be too unfamiliar to you.
And behind the scenes in terms of our database, we're going to be reading from our database.
So we're going to need to tap into our model that contains all our articles and we're going to find
the documents that match a particular condition. In this case because we want to fetch all of the articles
in our articles collection
we're actually going to leave that condition empty and that will find us everything inside a particular
collection.
And then we have our callback which can callback with an error
and also the results of that search which will contain all of the documents inside our articles collection.
So let's put that theory into practice and write some code in our app.js.
So right here where you see the // our TODO comment we're going to delete that and
we're going to do what we need to do which is create our get route that fetches all of the articles.
So let's tap in to our app and then use the get method from Express and then we're going to specify
the route which is going to be the entire collection of articles.
So we're going to put that at /articles and then we're going to have our callback, our request
and response. And then inside our callback is where we're going to query our database and find all of
the documents inside the articles collection.
And to do that we're going to specify the model name and use the find method.
So we'll say article which is the name of our model .find which is the name of the method and then
we're going to leave the first parameter empty because we actually don't have any conditions. We just want
to find all of them. And then we're just going to add our callback which can come back with an error
and also the results of the find method.
So first one we'll call error and the other one we'll call foundArticles or whatever it is you want to
call it as long as you know what it means.
So now we're going to trawl through all our articles find everything that's in there.
Then once it's done it's going to trigger the callback potentially show us any errors if there were
some but otherwise just allow us access to the found articles.
So now if we simply use our log and log the foundArticles and close off this method, then if we hit
save our server should work fine.
But it seems like I've got a typo here, instead of userNewUrlParser, it should be useNew
UrlParser.
I'm just going to copy that in to make sure I don't have any more typos there.
So now if we restart our server you can see we no longer have any warnings or any deprecation warnings
or any problems there.
So now our server has started on port 3000 and in order to trigger our app.get method
we need to try and access our localhost:3000/articles.
So let's go into Chrome and go into a localhost port 3000/articles. And this will
trigger our get requests so you can see it's running
and now if we check into our terminal you can see it's logged all of the documents that are currently
contained in our articles collection.
Now if we didn't want it to just log it and instead we wanted to send it back to the client then instead of
console logging the found articles we would say res.send.
So the results are going to be sending all of these found articles. And now our server has refreshed
and we're going to tap into this get again.
And you can see we now have all of our documents printed over here.
And because I've got that Chrome extension JSON Awesome Viewer installed to my Chrome browser you can
see it's actually rendering it in quite a nice pretty way
and we can see all four items in drop down menu format.
And now you should get to see this is getting pretty similar to when you access a public API and try
to access a resource by making a get request.
So now let's head back into Atom.
Now the last thing we're going to do before we wrap up the get on our articles route is to actually
just added an error check in here
right?
So just in case we had an error we want to be able to reflect that back to the client.
So inside this callback we're going to say if there were no errors then in that case we're going to
res.send the found articles, but if there were errors we're going to res.send the errors.
And this way it just means that if we have a problem we can see it on the client side instead of it
being lost to the ether.
So that is all the code we need to write to set up our first get routes.
And we've now successfully tackled this first one in the table where we handle when a client wants to
get the resources at the articles route.