WEBVTT 0 00:00.730 --> 00:00.950 All right. 1 00:00.960 --> 00:08.250 So now that we're ready to create are fully fledged RESTful API let's revisit this table that we saw 2 00:08.250 --> 00:16.230 previously where we have the HTTP verbs on the left those five trusty verbs: get, post, put, patch 3 00:16.320 --> 00:23.760 and delete. And then we've got our RESTful routing over here. One which is for the overall category for 4 00:23.790 --> 00:30.930 all of the articles that give me access through our API and then one which is for a specific resource 5 00:31.050 --> 00:33.290 inside that collection. 6 00:33.300 --> 00:37.730 So in this case we're using an article on Jack Bauer as the example. 7 00:37.740 --> 00:45.390 Now in order to make our API RESTful, we're going to go through this table and create each of these routes 8 00:45.510 --> 00:48.510 in turn and we're going to build it up from scratch. 9 00:48.780 --> 00:54.300 And the first thing we're going to tackle is of course our get route which fetches all of the articles 10 00:54.630 --> 01:03.420 using the /articles route. So we can do this using Express by writing app.get specifying 11 01:03.480 --> 01:10.380 our route and then adding a callback function which can tackle the request and response. 12 01:10.380 --> 01:15.690 And we've seen this and done this a few times now so it shouldn't be too unfamiliar to you. 13 01:16.320 --> 01:21.930 And behind the scenes in terms of our database, we're going to be reading from our database. 14 01:21.930 --> 01:28.320 So we're going to need to tap into our model that contains all our articles and we're going to find 15 01:28.470 --> 01:34.710 the documents that match a particular condition. In this case because we want to fetch all of the articles 16 01:34.860 --> 01:36.540 in our articles collection 17 01:36.540 --> 01:44.280 we're actually going to leave that condition empty and that will find us everything inside a particular 18 01:44.280 --> 01:45.710 collection. 19 01:45.750 --> 01:49.440 And then we have our callback which can callback with an error 20 01:49.740 --> 01:59.640 and also the results of that search which will contain all of the documents inside our articles collection. 21 01:59.670 --> 02:04.860 So let's put that theory into practice and write some code in our app.js. 22 02:04.870 --> 02:10.950 So right here where you see the // our TODO comment we're going to delete that and 23 02:10.950 --> 02:17.460 we're going to do what we need to do which is create our get route that fetches all of the articles. 24 02:17.910 --> 02:24.690 So let's tap in to our app and then use the get method from Express and then we're going to specify 25 02:24.690 --> 02:28.890 the route which is going to be the entire collection of articles. 26 02:28.920 --> 02:35.610 So we're going to put that at /articles and then we're going to have our callback, our request 27 02:35.700 --> 02:44.040 and response. And then inside our callback is where we're going to query our database and find all of 28 02:44.040 --> 02:48.450 the documents inside the articles collection. 29 02:48.450 --> 02:52.890 And to do that we're going to specify the model name and use the find method. 30 02:52.980 --> 03:01.800 So we'll say article which is the name of our model .find which is the name of the method and then 31 03:01.800 --> 03:07.710 we're going to leave the first parameter empty because we actually don't have any conditions. We just want 32 03:07.710 --> 03:13.540 to find all of them. And then we're just going to add our callback which can come back with an error 33 03:13.650 --> 03:16.470 and also the results of the find method. 34 03:17.070 --> 03:24.180 So first one we'll call error and the other one we'll call foundArticles or whatever it is you want to 35 03:24.180 --> 03:27.780 call it as long as you know what it means. 36 03:27.780 --> 03:32.190 So now we're going to trawl through all our articles find everything that's in there. 37 03:32.310 --> 03:36.990 Then once it's done it's going to trigger the callback potentially show us any errors if there were 38 03:36.990 --> 03:41.970 some but otherwise just allow us access to the found articles. 39 03:41.970 --> 03:51.240 So now if we simply use our log and log the foundArticles and close off this method, then if we hit 40 03:51.260 --> 03:54.180 save our server should work fine. 41 03:54.270 --> 04:01.640 But it seems like I've got a typo here, instead of userNewUrlParser, it should be useNew 42 04:01.660 --> 04:02.440 UrlParser. 43 04:02.460 --> 04:06.820 I'm just going to copy that in to make sure I don't have any more typos there. 44 04:06.840 --> 04:12.660 So now if we restart our server you can see we no longer have any warnings or any deprecation warnings 45 04:12.690 --> 04:14.550 or any problems there. 46 04:14.580 --> 04:21.000 So now our server has started on port 3000 and in order to trigger our app.get method 47 04:21.360 --> 04:27.030 we need to try and access our localhost:3000/articles. 48 04:27.030 --> 04:37.170 So let's go into Chrome and go into a localhost port 3000/articles. And this will 49 04:37.170 --> 04:40.160 trigger our get requests so you can see it's running 50 04:40.200 --> 04:45.870 and now if we check into our terminal you can see it's logged all of the documents that are currently 51 04:45.870 --> 04:49.320 contained in our articles collection. 52 04:49.320 --> 04:55.350 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 53 04:55.350 --> 05:00.160 console logging the found articles we would say res.send. 54 05:00.170 --> 05:08.770 So the results are going to be sending all of these found articles. And now our server has refreshed 55 05:08.890 --> 05:12.850 and we're going to tap into this get again. 56 05:12.970 --> 05:18.460 And you can see we now have all of our documents printed over here. 57 05:18.460 --> 05:25.570 And because I've got that Chrome extension JSON Awesome Viewer installed to my Chrome browser you can 58 05:25.570 --> 05:29.410 see it's actually rendering it in quite a nice pretty way 59 05:29.410 --> 05:34.600 and we can see all four items in drop down menu format. 60 05:34.600 --> 05:42.050 And now you should get to see this is getting pretty similar to when you access a public API and try 61 05:42.050 --> 05:46.900 to access a resource by making a get request. 62 05:47.290 --> 05:50.800 So now let's head back into Atom. 63 05:50.980 --> 05:57.670 Now the last thing we're going to do before we wrap up the get on our articles route is to actually 64 05:57.670 --> 06:00.010 just added an error check in here 65 06:00.010 --> 06:00.600 right? 66 06:00.610 --> 06:05.570 So just in case we had an error we want to be able to reflect that back to the client. 67 06:05.650 --> 06:14.170 So inside this callback we're going to say if there were no errors then in that case we're going to 68 06:14.170 --> 06:23.130 res.send the found articles, but if there were errors we're going to res.send the errors. 69 06:23.130 --> 06:28.630 And this way it just means that if we have a problem we can see it on the client side instead of it 70 06:28.630 --> 06:30.840 being lost to the ether. 71 06:30.850 --> 06:36.520 So that is all the code we need to write to set up our first get routes. 72 06:36.580 --> 06:43.540 And we've now successfully tackled this first one in the table where we handle when a client wants to 73 06:43.660 --> 06:47.830 get the resources at the articles route.