1 00:00:00,620 --> 00:00:05,320 Remember when we wrote out first route by hand and had to use a request and 2 00:00:05,320 --> 00:00:08,160 response object for the route handler. 3 00:00:08,160 --> 00:00:11,820 We've been using the response object to return a response to the client. 4 00:00:12,850 --> 00:00:16,850 But we haven't been using the request object yet, apart from declaring it. 5 00:00:18,510 --> 00:00:21,850 Let's look at the express documentation for the request object. 6 00:00:22,870 --> 00:00:28,490 There are quite a few properties that you can access on the request. 7 00:00:28,490 --> 00:00:33,090 The one we're most interested in is the body property. 8 00:00:33,090 --> 00:00:36,690 This is where our form response will end up. 9 00:00:36,690 --> 00:00:41,470 Notice though, that by default it is undefined. 10 00:00:41,470 --> 00:00:47,510 To put the form responses into the body, we'll need what is called Middleware. 11 00:00:47,510 --> 00:00:51,460 Middleware is basically some code that fits in the middle of a request or 12 00:00:51,460 --> 00:00:53,230 a response. 13 00:00:53,230 --> 00:00:56,670 Middleware does something to the incoming data and 14 00:00:56,670 --> 00:01:00,350 hands the result of to the rest of your application. 15 00:01:00,350 --> 00:01:04,260 We'll look more deeply at Middleware later in this course. 16 00:01:04,260 --> 00:01:06,700 But we don't really need to know a lot about it yet. 17 00:01:06,700 --> 00:01:10,510 I will show you how to install some middleware in a moment. 18 00:01:10,510 --> 00:01:11,700 First, though, 19 00:01:11,700 --> 00:01:16,506 let's use the console to take a look at the request object more closely. 20 00:01:16,506 --> 00:01:19,720 In the app.js file, 21 00:01:19,720 --> 00:01:24,560 I'll add a line in the post route to print out the request object to the console. 22 00:01:25,790 --> 00:01:31,028 I'll save it and then go back to the browser to create a new request. 23 00:01:31,028 --> 00:01:34,130 Our code will run. 24 00:01:34,130 --> 00:01:38,000 I don't need a name right now, and I'll click on Submit. 25 00:01:39,070 --> 00:01:41,690 Moving to the console where Express is running, 26 00:01:41,690 --> 00:01:46,280 you can see that the request object includes a lot of information. 27 00:01:46,280 --> 00:01:48,740 We will look too closely at the request. 28 00:01:48,740 --> 00:01:51,572 I just wanted you to get an appreciation for 29 00:01:51,572 --> 00:01:56,013 the amount of work expressed as to simplify our job as developers. 30 00:01:56,013 --> 00:01:58,776 Most of the time, we don't need many of these properties. 31 00:01:58,776 --> 00:02:01,090 We'll just access a select few. 32 00:02:02,310 --> 00:02:05,497 I'll clear this screen, and then we'll switch back to the app.js file. 33 00:02:06,570 --> 00:02:08,630 I'm just going to examine the body property now. 34 00:02:11,340 --> 00:02:19,200 If I submit the form now using my name And switch to the console, 35 00:02:19,200 --> 00:02:25,330 as you can see, I get undefined, just as the documents promised. 36 00:02:25,330 --> 00:02:28,560 Let's install the middleware, called for by the docs. 37 00:02:30,200 --> 00:02:34,760 It's called body parser and it will process the incoming form submission data 38 00:02:34,760 --> 00:02:37,840 into a format that's easier for our program to read. 39 00:02:39,240 --> 00:02:45,329 I'll go to the terminal and then I'll type, 40 00:02:45,329 --> 00:02:49,184 npm install body-parser. 41 00:02:51,582 --> 00:02:56,350 When that's done, I can require it in my app.js file at the top. 42 00:03:09,781 --> 00:03:11,910 Now, we'll tell Express to use it. 43 00:03:12,915 --> 00:03:17,740 Body-parser contains several parses, the different ways the clients can send data. 44 00:03:19,140 --> 00:03:24,520 HTML forms normally encode the data they send the same way URLs do. 45 00:03:25,910 --> 00:03:30,080 So, we'll need to use the urlencode parser. 46 00:03:32,880 --> 00:03:37,596 We'll pass in an object, turning 47 00:03:37,596 --> 00:03:42,644 off the parser's extended option. 48 00:03:46,412 --> 00:03:49,263 Don“t worry too much about this line of code. 49 00:03:49,263 --> 00:03:50,670 To be honest, 50 00:03:50,670 --> 00:03:56,040 I have to look this up everytime I need to include it in my Express applications. 51 00:03:56,040 --> 00:03:59,390 I've included more information about how forms and data and 52 00:03:59,390 --> 00:04:01,800 how body-parser works in the teacher's notes. 53 00:04:03,360 --> 00:04:04,790 Now, let's submit a name. 54 00:04:06,810 --> 00:04:12,160 In the console, you can see the body property has now a username property. 55 00:04:14,380 --> 00:04:19,390 Our app is now able to understand the information it's getting from our form. 56 00:04:19,390 --> 00:04:24,350 Next, we'll use that information to send something meaningful back to the client. 57 00:04:24,350 --> 00:04:29,140 While we do this, let's take a closer look at the response objects in the next video.