1 00:00:00,000 --> 00:00:01,620 Instructor: In the last section 2 00:00:01,620 --> 00:00:03,570 we started our server up and we've got our 3 00:00:03,570 --> 00:00:07,050 skeleton index.js file available to us now. 4 00:00:07,050 --> 00:00:08,760 As we're going through all the express code 5 00:00:08,760 --> 00:00:10,110 in this section 6 00:00:10,110 --> 00:00:11,280 the entire course that we're going 7 00:00:11,280 --> 00:00:13,332 through here is a lot more 8 00:00:13,332 --> 00:00:15,540 about React and Redux than is about express 9 00:00:15,540 --> 00:00:17,310 and so we're gonna kind of speed through 10 00:00:17,310 --> 00:00:19,830 a lot of the express stuff and focus much more 11 00:00:19,830 --> 00:00:24,090 on the concepts as opposed to the syntax. 12 00:00:24,090 --> 00:00:25,500 So again as we're going through here 13 00:00:25,500 --> 00:00:27,000 it's gonna be a lot more about the concepts 14 00:00:27,000 --> 00:00:28,420 not necessarily the syntax 15 00:00:29,298 --> 00:00:32,130 and so as far as syntax goes I'm gonna kind of speed 16 00:00:32,130 --> 00:00:33,750 through it a little bit because again 17 00:00:33,750 --> 00:00:36,330 it's not the focus of this course. 18 00:00:36,330 --> 00:00:38,190 Not to say that we won't wanna explain it at all 19 00:00:38,190 --> 00:00:39,940 but I just want you to be prepared. 20 00:00:41,160 --> 00:00:43,710 So we're in our index.js file here 21 00:00:43,710 --> 00:00:45,330 and the first thing I want to make very clear 22 00:00:45,330 --> 00:00:47,610 to you is that we do not have access 23 00:00:47,610 --> 00:00:52,203 to the full gamut of ES6 syntax available right now. 24 00:00:53,310 --> 00:00:55,260 At the time of writing this section 25 00:00:55,260 --> 00:00:57,773 I'm using node version 5.7 26 00:00:57,773 --> 00:01:01,031 and 5.7 still has a pretty hefty number 27 00:01:01,031 --> 00:01:05,730 of features that are not included with this version of Node. 28 00:01:05,730 --> 00:01:09,510 So for example I don't have access to import statements. 29 00:01:09,510 --> 00:01:11,190 So instead of import statements 30 00:01:11,190 --> 00:01:14,490 we will use the require syntax instead. 31 00:01:14,490 --> 00:01:16,743 Let's see what that looks like. 32 00:01:18,760 --> 00:01:21,960 All right express, require, express, 33 00:01:21,960 --> 00:01:23,160 and so this is how we're going to 34 00:01:23,160 --> 00:01:25,560 be doing code sharing between files. 35 00:01:25,560 --> 00:01:28,323 We're going to be using the require keyword instead. 36 00:01:29,610 --> 00:01:32,280 So I'm going to need to require a couple files, 37 00:01:32,280 --> 00:01:34,030 couple different libraries in here. 38 00:01:34,950 --> 00:01:37,500 First we'll start off with express. 39 00:01:37,500 --> 00:01:41,750 I'm also going to import http and we're gonna talk 40 00:01:41,750 --> 00:01:44,343 about what each of these are very shortly. 41 00:01:45,360 --> 00:01:47,630 I'm going to grab bodyParser. 42 00:01:53,430 --> 00:01:55,413 Let's also get access to Morgan. 43 00:01:58,680 --> 00:02:00,750 Okay so these are basically libraries 44 00:02:00,750 --> 00:02:02,610 that we just installed except for 45 00:02:02,610 --> 00:02:05,010 I think we also grabbed mongoose as well 46 00:02:05,010 --> 00:02:08,106 but we haven't required it into this directory just yet. 47 00:02:08,106 --> 00:02:08,939 That's fine. 48 00:02:08,939 --> 00:02:12,060 Let's go ahead and go through our app setup 49 00:02:12,060 --> 00:02:15,363 and then we're also gonna do some server setup as well. 50 00:02:16,950 --> 00:02:19,440 So stuff under this app setup section is all 51 00:02:19,440 --> 00:02:21,810 about getting express the way we're working 52 00:02:21,810 --> 00:02:23,130 that we want it to, 53 00:02:23,130 --> 00:02:25,560 anything that goes underneath the server setup 54 00:02:25,560 --> 00:02:26,393 is talking more 55 00:02:26,393 --> 00:02:28,860 about getting our express application to talk 56 00:02:28,860 --> 00:02:30,870 to the outside world. 57 00:02:30,870 --> 00:02:32,880 Let's do the server setup first 58 00:02:32,880 --> 00:02:35,040 because it's just a little bit easier. 59 00:02:35,040 --> 00:02:37,620 The first thing we need to do is define a port 60 00:02:37,620 --> 00:02:41,100 that our server is going to run on on our local machine. 61 00:02:41,100 --> 00:02:46,100 So I'm gonna say const port is going to be process.env.PORT 62 00:02:46,411 --> 00:02:48,813 or 3090. 63 00:02:49,800 --> 00:02:51,690 So what this line here does is it says 64 00:02:51,690 --> 00:02:55,560 if there's an environment variable of PORT already defined 65 00:02:55,560 --> 00:02:58,851 use that otherwise use the port 3090, 66 00:02:58,851 --> 00:03:01,050 and so by default we're probably gonna end 67 00:03:01,050 --> 00:03:03,960 up using this 3090 right here most of the time. 68 00:03:03,960 --> 00:03:04,830 Unless you've set up 69 00:03:04,830 --> 00:03:08,520 your local machine to define a PORT ahead of time. 70 00:03:08,520 --> 00:03:11,370 If you don't wanna use PORT 3090 here go ahead and set it 71 00:03:11,370 --> 00:03:13,020 to whatever you'd like. 72 00:03:13,020 --> 00:03:15,390 Just be aware that whenever I say 3090, 73 00:03:15,390 --> 00:03:18,183 you're gonna be needing to use your local PORT instead. 74 00:03:19,140 --> 00:03:21,310 So once we've got a PORT put together 75 00:03:22,155 --> 00:03:23,070 we're going to 76 00:03:23,070 --> 00:03:27,540 create an app at the top. 77 00:03:27,540 --> 00:03:29,490 So we're gonna go back up to the top here. 78 00:03:29,490 --> 00:03:34,170 I'm gonna create an instance of express. 79 00:03:34,170 --> 00:03:36,480 So we take our express and we create an instance 80 00:03:36,480 --> 00:03:37,653 which is our app. 81 00:03:39,420 --> 00:03:43,110 Next back down at the bottom underneath server setup 82 00:03:43,110 --> 00:03:46,260 we're gonna say const server is http 83 00:03:46,260 --> 00:03:49,590 create server with app. 84 00:03:49,590 --> 00:03:51,690 So the http library right here, 85 00:03:51,690 --> 00:03:54,450 this is a native node library, 86 00:03:54,450 --> 00:03:57,810 is library that is for working very low level 87 00:03:57,810 --> 00:04:00,660 with http requests that are incoming. 88 00:04:00,660 --> 00:04:02,760 What this line right here does is it says 89 00:04:02,760 --> 00:04:07,170 create an http server that knows how to receive requests 90 00:04:07,170 --> 00:04:08,700 and anything that comes in, 91 00:04:08,700 --> 00:04:09,780 go ahead and forward it 92 00:04:09,780 --> 00:04:11,910 onto our express application, 93 00:04:11,910 --> 00:04:14,702 and this app right here is what we're going to 94 00:04:14,702 --> 00:04:16,552 be adding functionality to over time. 95 00:04:17,940 --> 00:04:21,329 Once we create the server we will then tell the server to 96 00:04:21,329 --> 00:04:25,493 listen to the port we just declared right above and then 97 00:04:25,493 --> 00:04:30,493 we will console.log simply Server listening on, port. 98 00:04:34,230 --> 00:04:38,163 Make sure the port is outside of the quote right here. 99 00:04:39,450 --> 00:04:40,283 Okay so let's go ahead 100 00:04:40,283 --> 00:04:42,480 and save this and let's see how we're doing 101 00:04:42,480 --> 00:04:44,253 if we try to run this file. 102 00:04:45,240 --> 00:04:46,440 Back over my terminal 103 00:04:46,440 --> 00:04:49,383 I'm going to run node index.js 104 00:04:49,383 --> 00:04:54,383 and I now get server listening on 3090. 105 00:04:54,390 --> 00:04:55,380 Cool. 106 00:04:55,380 --> 00:04:57,150 Let's see what happens when we try to 107 00:04:57,150 --> 00:05:00,300 visit local host 3090. 108 00:05:00,300 --> 00:05:03,750 I'm gonna pull open my browser and just so it's clear 109 00:05:03,750 --> 00:05:05,160 I'm gonna repeat this again. 110 00:05:05,160 --> 00:05:08,250 To get access to our server or the location that our server 111 00:05:08,250 --> 00:05:12,900 is running at is local host colon 3090. 112 00:05:12,900 --> 00:05:15,990 So I've got localhost:3090. 113 00:05:15,990 --> 00:05:17,040 So I'm gonna go there 114 00:05:17,040 --> 00:05:19,830 and I get basically an error message that says cannot 115 00:05:19,830 --> 00:05:22,560 get forward slash, which is totally fine. 116 00:05:22,560 --> 00:05:25,837 Basically what the server is telling us right now is, 117 00:05:25,837 --> 00:05:28,380 "Hey you're trying to get this forward slash route 118 00:05:28,380 --> 00:05:30,660 but I have nothing to show you." 119 00:05:30,660 --> 00:05:32,010 Nothing is set up yet. 120 00:05:32,010 --> 00:05:34,890 So that's totally fine to get an error message right there. 121 00:05:34,890 --> 00:05:37,477 So this is looking good for our initial setup. 122 00:05:37,477 --> 00:05:39,540 Let's continue with our app set up in the next section. 123 00:05:39,540 --> 00:05:40,490 I'll see you there.