1 00:00:00,960 --> 00:00:05,290 We've now done some initial setup and now it's time to actually start writing out our services. 2 00:00:05,340 --> 00:00:09,870 But this initial implementation we're not going to worry too much about anything micros services or 3 00:00:09,870 --> 00:00:10,890 anything like that. 4 00:00:10,910 --> 00:00:15,370 We're really just going to put together some very basic functionality for the posts in comment service. 5 00:00:15,390 --> 00:00:22,920 So in other words some very basic express J.S. code to manipulate or deal with some very basic resources. 6 00:00:22,920 --> 00:00:25,530 We're going to first begin with our post service. 7 00:00:25,530 --> 00:00:30,390 Whenever you start designing a service or putting it together it really is worth a time to think very 8 00:00:30,390 --> 00:00:33,810 critically about exactly what you want your service to do. 9 00:00:33,870 --> 00:00:37,100 So in our case we know that we want to create a post and list all posts. 10 00:00:37,200 --> 00:00:42,240 Well let's get a little bit deeper technical detail here and really truly understand what this thing 11 00:00:42,240 --> 00:00:42,910 needs to do. 12 00:00:43,710 --> 00:00:46,530 So here's my thought for the Postal Service. 13 00:00:46,530 --> 00:00:52,650 I think that we need to have a single route that's going to be in charge of retrieving all the different 14 00:00:52,650 --> 00:00:54,650 posted have ever been created. 15 00:00:54,660 --> 00:00:59,840 I think we also need one distinct route that is can be in charge of creating a brand new post. 16 00:00:59,960 --> 00:01:04,750 We're going to have one route of us if we make a GET request to it we'll get all of our posts. 17 00:01:04,920 --> 00:01:10,560 We'll have another one of slash posts if we make a post to it with somebody that has a title that it 18 00:01:10,620 --> 00:01:11,500 is a string. 19 00:01:11,500 --> 00:01:12,980 We're going to create a new post. 20 00:01:13,140 --> 00:01:17,940 So that's what we're going to use as our guide for implementing our post service. 21 00:01:17,940 --> 00:01:21,760 Let's open up our code editor inside the Post's project directory right now. 22 00:01:21,760 --> 00:01:25,890 We're going to start to build out a very small express application that's going to implement these two 23 00:01:25,890 --> 00:01:28,490 routes back at my terminal. 24 00:01:28,510 --> 00:01:32,830 You'll notice I've redone my layout here just you can see these different terminal windows a little 25 00:01:32,830 --> 00:01:39,140 bit more easily I'm going to find my post terminal window and open up my code editor inside their 26 00:01:42,490 --> 00:01:49,740 and then once inside they're going to do a initial file I'm going to make right away of index that J.S. 27 00:01:50,260 --> 00:01:55,600 we're gonna write out just about all the code for application inside this one single file at the very 28 00:01:55,600 --> 00:01:55,930 top. 29 00:01:55,930 --> 00:02:01,090 We're going to get access to express we're gonna create new express application we're gonna set up those 30 00:02:01,090 --> 00:02:05,620 two different route handlers so we just discussed it's all gonna ease some pretty straightforward express 31 00:02:05,620 --> 00:02:09,620 related stuff so let's get to it a little bit of typing here. 32 00:02:09,750 --> 00:02:15,410 So first off we will require an express I'll create a new app 33 00:02:18,400 --> 00:02:22,110 and then I'm going to associate those two different routes with the app we just created. 34 00:02:22,480 --> 00:02:30,560 So we'll do an app get to slash posts and I'll put in my root handler and then if we ever make a post 35 00:02:30,560 --> 00:02:39,430 request to slash posts and we'll put in rec and rez as well then at the bottom right away I'm going 36 00:02:39,430 --> 00:02:45,310 to make sure that my express application listens on a very specific port so I'm gonna put in here app 37 00:02:45,410 --> 00:02:54,090 dot listen we're gonna have this first service listen on Port 4000 and then inside that callback we'll 38 00:02:54,090 --> 00:03:00,330 say console log listening on 4000. 39 00:03:00,430 --> 00:03:00,730 All right. 40 00:03:00,730 --> 00:03:02,890 Simple enough okay. 41 00:03:02,920 --> 00:03:08,050 So now let's start to implement these two different root handlers I mentioned in passing just a moment 42 00:03:08,050 --> 00:03:12,340 ago in previous video that we're not going to worry about any database or anything like that with these 43 00:03:12,340 --> 00:03:13,430 different services. 44 00:03:13,480 --> 00:03:16,580 So we're going to store all these different resources in memory. 45 00:03:16,600 --> 00:03:20,650 The downside to this approach is that anytime that we restart a service we're going to lose all of our 46 00:03:20,650 --> 00:03:21,120 data. 47 00:03:21,220 --> 00:03:27,060 But that's totally okay for this initial little toy project we're putting together right after I create 48 00:03:27,060 --> 00:03:31,800 my express application I'm going to make a little object that's going to be in charge of storing all 49 00:03:31,800 --> 00:03:33,690 the different posts that get created. 50 00:03:33,810 --> 00:03:40,230 We'll say concert us is an empty object like so that this object is where we're going to store every 51 00:03:40,230 --> 00:03:45,000 post that we create now that we've got this kind of repository for all the different post we're going 52 00:03:45,000 --> 00:03:45,530 to store. 53 00:03:45,540 --> 00:03:48,090 We can start to implement the first row handle right here. 54 00:03:48,120 --> 00:03:53,220 That's going to retrieve all the different posted have been created very easily in just say inside that 55 00:03:53,220 --> 00:03:54,350 root handler. 56 00:03:54,720 --> 00:03:56,950 Let's send all of our posts. 57 00:03:57,090 --> 00:04:02,710 Now if anyone makes a request to slash posts we're gonna send back all the posts have been created the 58 00:04:02,900 --> 00:04:06,260 next we have to do is make sure that we implement the ability to create a new post. 59 00:04:06,290 --> 00:04:11,450 So we have to add in some implementation for the post request handler to slash posts. 60 00:04:11,450 --> 00:04:17,290 Remember that we probably want to eventually have some ideas associated with every post that gets created. 61 00:04:17,330 --> 00:04:21,800 Right now we don't really have any ability to create a idea of any sort. 62 00:04:21,800 --> 00:04:27,200 So let's add in a little bit of code to randomly generate an idea whenever someone makes a post request 63 00:04:27,200 --> 00:04:29,410 to slash post right here with the intent to create a post. 64 00:04:29,480 --> 00:04:35,150 We're going to randomly generate an I.D. assign it to the object that the user just sent to us and then 65 00:04:35,150 --> 00:04:36,900 store that entire thing. 66 00:04:36,900 --> 00:04:38,330 That's a little bit confusing for me to say. 67 00:04:38,390 --> 00:04:42,580 So let me just show you exactly what we're going to do at the very top of the file. 68 00:04:42,620 --> 00:04:44,970 I'm going to add in an additional require statement. 69 00:04:45,140 --> 00:04:52,600 I'm going to require in random bytes from the crypto package. 70 00:04:52,750 --> 00:04:56,830 We're going to use random bytes right here to generate a new idea that we're going to assign to the 71 00:04:56,830 --> 00:04:59,220 posts that the user is trying to create. 72 00:04:59,230 --> 00:05:05,110 So down inside the post request handler and say const I.D. We're gonna generate our random idea right 73 00:05:05,110 --> 00:05:11,860 here and we'll do that by using the random bytes function we'll say random bytes. 74 00:05:12,130 --> 00:05:13,940 I want four bytes of random data. 75 00:05:13,970 --> 00:05:17,400 So this is just you get a random string for us that's all we're doing here. 76 00:05:17,650 --> 00:05:19,790 We'll say to string x. 77 00:05:19,940 --> 00:05:24,010 So that's going to give us a nice random looking I.D. It looks something like that. 78 00:05:24,070 --> 00:05:26,070 It'll be actually x. 79 00:05:26,110 --> 00:05:30,190 So we won't have like k's in there and ages and whatnot but you get the idea that's pretty much what 80 00:05:30,190 --> 00:05:36,270 we're going to have for our I.D. It's now the next you're going to do is take a look at the request 81 00:05:36,330 --> 00:05:38,660 that we've just received from the user. 82 00:05:38,670 --> 00:05:43,380 Remember we're making the assumption here that whenever a user makes a post request to this route they're 83 00:05:43,380 --> 00:05:47,410 going to send along a body that has a title property inside of it. 84 00:05:47,820 --> 00:05:49,700 So we're going to pull out that title. 85 00:05:49,830 --> 00:05:55,590 We're going to generate that or take that randomly generated I.D. and then store them altogether inside 86 00:05:55,590 --> 00:06:01,680 of our post object up here that we just created so let's get access to the title that the user just 87 00:06:01,680 --> 00:06:02,520 sent to us. 88 00:06:03,180 --> 00:06:12,430 Let's say Rick dot body will then add in a new e at the idea that we just generated on our posts object 89 00:06:13,150 --> 00:06:23,050 and I'll assign to that the idea and the title and then finally it down here at the bottom after we 90 00:06:23,050 --> 00:06:28,540 add in that brand new post let's send back a response to the user to let them know that hey we have 91 00:06:28,540 --> 00:06:30,430 now reached a new post for you. 92 00:06:30,460 --> 00:06:31,770 It's all good to go. 93 00:06:31,870 --> 00:06:38,800 I'll do a rez set a manual status here of 201 which indicates we just created a resource and then I'll 94 00:06:38,800 --> 00:06:46,290 send back those at I.D. which again is the post that we just created. 95 00:06:46,290 --> 00:06:46,520 All right. 96 00:06:46,530 --> 00:06:47,730 So this is looking pretty good. 97 00:06:47,730 --> 00:06:53,460 There's one last little thing we have to do we need to make sure that we add in a body parser to make 98 00:06:53,460 --> 00:06:57,570 sure that whenever a user sends us some Jason data in a body request actually gets passed. 99 00:06:57,600 --> 00:07:00,660 And so it actually shows up appropriately inside of a request handler. 100 00:07:00,690 --> 00:07:07,350 So just a bit of administration work with Express J.S. at the very top the file I'm going to add in 101 00:07:07,350 --> 00:07:09,450 a required statement for body parser 102 00:07:13,430 --> 00:07:18,540 and then right after we generate our app we'll do an app dot use body parser dot. 103 00:07:18,590 --> 00:07:21,400 Jason like so okay. 104 00:07:21,420 --> 00:07:25,650 And I should be at the code that we have here should be good to go. 105 00:07:25,680 --> 00:07:30,720 It implements both of these different features or these routes that we just discussed. 106 00:07:30,720 --> 00:07:34,770 The last thing we should do is add in a script to our package not just on file to start this project 107 00:07:34,770 --> 00:07:35,240 up. 108 00:07:35,280 --> 00:07:39,540 We can then take a quick break and then do some testing this thing in the next video. 109 00:07:39,560 --> 00:07:43,990 Go inside my package dot Jason File I'm gonna find the script section. 110 00:07:43,990 --> 00:07:50,800 I'm going to remove the default script inside their and I'll replace it with a start script that will 111 00:07:50,800 --> 00:07:56,780 run node on index dodges like so I'll save that. 112 00:07:57,000 --> 00:08:02,740 And then finally I'm going to go back over to my terminal I'll find my post terminal window and I'll 113 00:08:02,740 --> 00:08:08,610 do an NPM start inside their and I should see something says listening on 4000. 114 00:08:08,660 --> 00:08:09,120 Very good. 115 00:08:09,820 --> 00:08:10,170 Okay. 116 00:08:10,220 --> 00:08:14,360 So now that we've got this thing up and running our first little service Rick pause right here and we'll 117 00:08:14,360 --> 00:08:18,290 do a quick test of this thing in the next video just to make sure that's working as expected.