1 00:00:02,270 --> 00:00:07,910 As I mentioned before, developers are really, really good and making sure they don't keep doing the 2 00:00:07,910 --> 00:00:14,390 same thing over and over, and if there's anything that is done over and over, they build a tool for 3 00:00:14,390 --> 00:00:16,010 it so that it becomes simpler. 4 00:00:17,790 --> 00:00:21,840 Well, building servers was one of that, it's something that everybody uses. 5 00:00:23,410 --> 00:00:29,380 So people use libraries just like we used to react on the front end on top of JavaScript to make our 6 00:00:29,380 --> 00:00:33,220 lives easier and build faster, more robust servers. 7 00:00:34,350 --> 00:00:42,180 There are many libraries that people use with note to create a server, but the most popular hands down, 8 00:00:42,180 --> 00:00:44,780 the best one right now is Express. 9 00:00:45,060 --> 00:00:47,670 And I have here the state of JavaScript. 10 00:00:47,670 --> 00:00:53,790 Servais It's a famous survey that gets done every year at the end of the year that asks developers what 11 00:00:53,790 --> 00:00:56,670 their favorite tools are, what front and tools they use. 12 00:00:56,850 --> 00:00:58,920 And I highly recommend that you check it out. 13 00:00:59,190 --> 00:01:05,550 But he can see here for the back end, the library that is most popular is Xpress. 14 00:01:06,000 --> 00:01:09,680 And this purple bar means I've used it before and would use it again. 15 00:01:10,590 --> 00:01:16,440 There's other ones like Koha Media are happy, but the usage is very, very small. 16 00:01:17,490 --> 00:01:20,420 Express's just a tried and tested. 17 00:01:20,430 --> 00:01:24,390 There's plenty of tools and information online for it. 18 00:01:24,400 --> 00:01:29,030 So if you ever get stuck, you're able to find an answer to your issue right away. 19 00:01:29,190 --> 00:01:30,770 So expresses the way to go. 20 00:01:31,410 --> 00:01:38,250 So let's learn some express and show you how much easier it becomes to create our own server. 21 00:01:39,850 --> 00:01:46,840 So we're going to delete the server that we built previously in the last video and install Xpress, 22 00:01:47,170 --> 00:01:54,010 let's do NPM install and again through NPM we have the express package. 23 00:01:57,160 --> 00:02:03,790 And now we have express available to us, so let's grab it that way, you created an express server 24 00:02:04,420 --> 00:02:05,350 is very simple. 25 00:02:05,530 --> 00:02:08,680 You simply do express require. 26 00:02:10,380 --> 00:02:15,660 Express, we have to do a concert app and then to. 27 00:02:16,810 --> 00:02:21,280 Create that app to run express, we simply do express. 28 00:02:24,190 --> 00:02:26,380 And then finally abduct listen. 29 00:02:30,330 --> 00:02:33,210 And that's it, we have express running. 30 00:02:34,230 --> 00:02:35,700 Let me do npm start. 31 00:02:38,050 --> 00:02:42,190 And we see that our server is running. 32 00:02:43,360 --> 00:02:45,940 And if I go back to localhost three thousand. 33 00:02:47,020 --> 00:02:53,650 I have cannot get and that is because, well, again, we're not doing anything, but in just three 34 00:02:53,650 --> 00:02:59,860 lines we have a server and I want to show you that Express has great. 35 00:03:02,730 --> 00:03:07,730 A great guide to getting started and a ton of information of how to use it. 36 00:03:08,910 --> 00:03:13,950 And documentation for you that you can check out on your own, but for now, let's build our own. 37 00:03:16,730 --> 00:03:21,170 Well, Express's really, really good at doing around it, if you remember, in our localhost, we got 38 00:03:21,170 --> 00:03:22,310 cannot get. 39 00:03:24,000 --> 00:03:31,200 To do a get request with Express, we simply do not get in the first parameter is the path. 40 00:03:31,260 --> 00:03:32,670 We'll just do the route path. 41 00:03:34,930 --> 00:03:41,890 And the second parameter receives a request and a response and an express, instead of writing everything 42 00:03:41,890 --> 00:03:46,060 out, the common way of doing it is doing Rech and wrests. 43 00:03:48,190 --> 00:03:49,000 And in here. 44 00:03:50,170 --> 00:03:52,720 We can say response that sent. 45 00:03:54,360 --> 00:04:01,950 And we'll just send a hello, just like we did in our previous video, where we use the HTTP module 46 00:04:01,950 --> 00:04:03,090 that comes with node. 47 00:04:05,180 --> 00:04:06,620 So let's check that, let's see if that works. 48 00:04:07,510 --> 00:04:08,210 I'm going to save. 49 00:04:12,080 --> 00:04:13,610 And I get hello. 50 00:04:16,089 --> 00:04:20,649 If I open up the console here and the network tab and I refresh again. 51 00:04:22,330 --> 00:04:24,220 I see that the response. 52 00:04:25,770 --> 00:04:26,530 Yes, hello. 53 00:04:26,940 --> 00:04:28,380 We see content type. 54 00:04:29,930 --> 00:04:34,040 Textile mill that was converted automatically for us. 55 00:04:35,370 --> 00:04:40,440 I can also, instead of send just an e-mail. 56 00:04:42,100 --> 00:04:45,460 So instead of doing something like H1. 57 00:04:48,200 --> 00:04:49,370 And each one. 58 00:04:52,650 --> 00:04:55,390 Which gives us an H1 hello. 59 00:04:56,100 --> 00:04:58,410 I can also now send Jason. 60 00:04:59,580 --> 00:05:06,630 Const user again, and this time the user name will be Sally. 61 00:05:14,060 --> 00:05:15,640 Will be soccer. 62 00:05:17,050 --> 00:05:21,790 And I need to add an equal here and then in user. 63 00:05:24,480 --> 00:05:27,690 If I save that and go back and refresh. 64 00:05:29,050 --> 00:05:37,420 Look at that, it automatically does the JSON string if for me and returns a JSON content type, an 65 00:05:37,420 --> 00:05:41,230 application JSON content type with the response that is Jason. 66 00:05:43,210 --> 00:05:48,790 Very cool, so you can see the power of Express, even just from the simple example, it takes away 67 00:05:48,790 --> 00:05:53,690 a lot of the repetition and makes our life a whole lot easier. 68 00:05:54,670 --> 00:06:00,010 Now, we've only done get here, but you can also do post requests. 69 00:06:00,220 --> 00:06:05,770 So, for example, if I did post profile, well, now if I refresh. 70 00:06:08,030 --> 00:06:09,740 I get an error because. 71 00:06:12,670 --> 00:06:19,990 Because now I've made the request method, which was get from the Bouser, but here I only. 72 00:06:21,020 --> 00:06:23,120 Accept a post request. 73 00:06:24,700 --> 00:06:26,650 From Profar, if I do. 74 00:06:33,860 --> 00:06:42,590 I get cannot get profiled because even though we are going to the proper you, Earl, now we're doing 75 00:06:42,590 --> 00:06:43,400 a get request. 76 00:06:43,400 --> 00:06:46,370 Any time we refresh your browser, it's a get request. 77 00:06:47,000 --> 00:06:52,100 And in order to do that, we need to actually send some data over. 78 00:06:52,550 --> 00:06:56,210 And I'll show you how to do that in the next couple of videos. 79 00:06:56,540 --> 00:07:06,170 But just to show you how Express works, I can keep adding these routes less to get a profile that returns. 80 00:07:11,090 --> 00:07:12,410 Getting profile. 81 00:07:13,880 --> 00:07:15,440 And if I do a. 82 00:07:19,530 --> 00:07:25,110 Another get this time to the root, Yoro, and this one, I'll just say getting. 83 00:07:27,810 --> 00:07:31,980 If I say this now and refresh profile. 84 00:07:33,120 --> 00:07:37,530 I'll get a two hundred, OK, with the response of getting Profar. 85 00:07:38,970 --> 00:07:44,160 And if I do no profile and just the URL, I get root. 86 00:07:47,180 --> 00:07:51,000 And the way Express works is it checks each one line by line. 87 00:07:52,040 --> 00:08:02,360 So, for example, it will check, hey, did the request have the route URL, did the get request, 88 00:08:02,360 --> 00:08:05,840 have profile, did the post request, have profile? 89 00:08:06,200 --> 00:08:08,130 And if it matches any of these. 90 00:08:08,510 --> 00:08:12,710 Well, it will run whatever the function inside of it is. 91 00:08:13,190 --> 00:08:14,480 If I do a U. 92 00:08:14,480 --> 00:08:15,830 RL that doesn't exist. 93 00:08:18,000 --> 00:08:22,200 I get a four or four not fund because, well. 94 00:08:24,550 --> 00:08:28,240 We did a get request to this URL that doesn't exist. 95 00:08:29,290 --> 00:08:38,110 So using this, we can use the get method, the post method, the put method and the delete method that 96 00:08:38,110 --> 00:08:41,010 we're going to actually demonstrate in the next couple of videos. 97 00:08:41,470 --> 00:08:47,770 But as you can see, it's a simple matter of saying if you go to this route, do this, if you go to 98 00:08:47,770 --> 00:08:52,690 this one, do this and everything is just logical and trickles all the way down. 99 00:08:54,240 --> 00:08:57,120 All right, I'll see in the next video, bye bye.