1
00:00:02,140 --> 00:00:08,540
To parse incoming data, again we need some data and I'll get it from the pure Node.js file,

2
00:00:08,560 --> 00:00:14,490
I will simply copy this response here with the back ticks and replace my response here,

3
00:00:14,500 --> 00:00:21,030
this string with that copied string, so that again I try to output a user name and I have a form here.

4
00:00:21,130 --> 00:00:23,720
Now the user name is not always set,

5
00:00:23,740 --> 00:00:30,400
so here I want to have a default user name and then also get a body though which allows me to get the

6
00:00:30,400 --> 00:00:33,010
user name from that, like this.

7
00:00:33,410 --> 00:00:38,030
However actually we can remove the if block because we'll get the user name out of the request body

8
00:00:38,030 --> 00:00:45,920
a bit differently. Instead of manually parsing the body like that, with Express.js we can use a middleware

9
00:00:46,160 --> 00:00:48,680
that does this body parsing for us.

10
00:00:49,010 --> 00:00:53,690
So before we do anything with the response, we could have another middleware where in the function we

11
00:00:53,690 --> 00:01:00,080
execute there, we have all the body parsing logic so that in the subsequent middlewares, we get the parsed

12
00:01:00,080 --> 00:01:06,320
body available. We could write our own function for that but thankfully, there are third-party packages

13
00:01:06,320 --> 00:01:12,800
which can be integrated into this Express middleware flow that do that for us and the very popular package

14
00:01:13,070 --> 00:01:16,940
for parsing the request body and adding it to the request object,

15
00:01:16,940 --> 00:01:22,800
so adding the parsed body to the request object is the body-parser package.

16
00:01:22,970 --> 00:01:28,910
You can install this with npm install body-parser --save to add an entry to package.json

17
00:01:29,510 --> 00:01:30,410
and by hitting enter

18
00:01:30,410 --> 00:01:33,920
this will now be installed and is made available in your project.

19
00:01:33,920 --> 00:01:36,040
Now we just have to import it here,

20
00:01:36,080 --> 00:01:44,170
body parser by requiring body-parser and now body parser and that's the convenient thing can

21
00:01:44,170 --> 00:01:47,420
simply be added here as a middleware,

22
00:01:47,680 --> 00:01:50,780
we just have to pick one of the parsing methods,

23
00:01:50,950 --> 00:01:58,210
for example JSON if we had JSON data or in our case here, URL encoded which is essentially the

24
00:01:58,210 --> 00:02:06,620
form data we will receive and if we do that, this package will add a default middleware,

25
00:02:06,640 --> 00:02:12,160
so it adds a function like this basically to the Express middleware flow where it parses and extracts

26
00:02:12,160 --> 00:02:20,530
the incoming body, the request body and adds the parsed body back into that body field on the request object,

27
00:02:21,250 --> 00:02:29,410
so that now here we could say we want to set the user name, which now can be a constant, equal to

28
00:02:29,440 --> 00:02:37,790
request.body.username because we gave our input here a name of user name,

29
00:02:37,790 --> 00:02:45,740
so this will be the key by which we can access the field value or unknown user, if that should be undefined

30
00:02:45,800 --> 00:02:47,350
for whatever reason.

31
00:02:47,690 --> 00:02:54,770
And now with that let's give it a try, let's restart our server by again running app.js again and what

32
00:02:54,770 --> 00:03:00,150
one thinks we should apply here because we get the warning here, we should add an options object to URL

33
00:03:00,150 --> 00:03:07,350
encoded and said extended to false there which just controls how the body is parsed and this

34
00:03:07,350 --> 00:03:09,000
default setting should be fine

35
00:03:09,000 --> 00:03:11,660
so now we can start this without getting a warning

36
00:03:12,000 --> 00:03:17,540
and now if we go into the URL again and hit enter here, don't reload but hit enter in the URL, you

37
00:03:17,540 --> 00:03:19,640
see hi unknown user and

38
00:03:19,660 --> 00:03:25,890
now if you enter for example Anna here, you see Anna here as an output and we do see that here because of

39
00:03:25,890 --> 00:03:28,830
that body parser package which does that.

40
00:03:28,890 --> 00:03:34,800
So this is how we can parse the body with the help of this package and with the help of Express which

41
00:03:34,800 --> 00:03:39,720
makes it possible to use this package and that's of course more convenient than manually parsing the

42
00:03:39,720 --> 00:03:45,570
body like that. With that we got a basic setup with Express.js but now it would also be nice to

43
00:03:45,810 --> 00:03:53,670
not just send back HTML code like this in line, in our Javascript file but maybe send back a separate

44
00:03:54,040 --> 00:04:01,050
HTML file or even a dynamic template file which renders HTML but which also gives us dynamic

45
00:04:01,050 --> 00:04:07,200
injection points, where we can inject dynamic content because that's what we typically want to do. We

46
00:04:07,200 --> 00:04:14,010
want to prepare some content on the server and then create an HTML file which has some static elements

47
00:04:14,010 --> 00:04:20,090
that always are the same and some dynamic parts which depend on the data we generated on the server

48
00:04:20,340 --> 00:04:21,820
and that's what we'll do next.
