To parse incoming data, again we need some data and I'll get it from the pure Node.js file,
I will simply copy this response here with the back ticks and replace my response here,
this string with that copied string, so that again I try to output a user name and I have a form here.
Now the user name is not always set,
so here I want to have a default user name and then also get a body though which allows me to get the
user name from that, like this.
However actually we can remove the if block because we'll get the user name out of the request body
a bit differently. Instead of manually parsing the body like that, with Express.js we can use a middleware
that does this body parsing for us.
So before we do anything with the response, we could have another middleware where in the function we
execute there, we have all the body parsing logic so that in the subsequent middlewares, we get the parsed
body available. We could write our own function for that but thankfully, there are third-party packages
which can be integrated into this Express middleware flow that do that for us and the very popular package
for parsing the request body and adding it to the request object,
so adding the parsed body to the request object is the body-parser package.
You can install this with npm install body-parser --save to add an entry to package.json
and by hitting enter
this will now be installed and is made available in your project.
Now we just have to import it here,
body parser by requiring body-parser and now body parser and that's the convenient thing can
simply be added here as a middleware,
we just have to pick one of the parsing methods,
for example JSON if we had JSON data or in our case here, URL encoded which is essentially the
form data we will receive and if we do that, this package will add a default middleware,
so it adds a function like this basically to the Express middleware flow where it parses and extracts
the incoming body, the request body and adds the parsed body back into that body field on the request object,
so that now here we could say we want to set the user name, which now can be a constant, equal to
request.body.username because we gave our input here a name of user name,
so this will be the key by which we can access the field value or unknown user, if that should be undefined
for whatever reason.
And now with that let's give it a try, let's restart our server by again running app.js again and what
one thinks we should apply here because we get the warning here, we should add an options object to URL
encoded and said extended to false there which just controls how the body is parsed and this
default setting should be fine
so now we can start this without getting a warning
and now if we go into the URL again and hit enter here, don't reload but hit enter in the URL, you
see hi unknown user and
now if you enter for example Anna here, you see Anna here as an output and we do see that here because of
that body parser package which does that.
So this is how we can parse the body with the help of this package and with the help of Express which
makes it possible to use this package and that's of course more convenient than manually parsing the
body like that. With that we got a basic setup with Express.js but now it would also be nice to
not just send back HTML code like this in line, in our Javascript file but maybe send back a separate
HTML file or even a dynamic template file which renders HTML but which also gives us dynamic
injection points, where we can inject dynamic content because that's what we typically want to do. We
want to prepare some content on the server and then create an HTML file which has some static elements
that always are the same and some dynamic parts which depend on the data we generated on the server
and that's what we'll do next.