Basic Node and Express - Use body-parser to Parse POST Requests

Tell us what’s happening:

I don’t understand what is happening with this challenge. I had to look at the hint an basically copy what was there, but I don’t understand what is going on.

There are various bit that confused me, but the main thing was mounting the middleware using app.use(bodyParser.urlencoded({ extended: false })),

What is it and how does it relate to app.use("/name", bodyParser.json());

###Your project link(s)

app.use(bodyParser.urlencoded({ extended: false }))
app.use("/name", bodyParser.json());

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Basic Node and Express - Use body-parser to Parse POST Requests

Here is a reference to URL encoding, in case you need more explanation of that.

app.use() will mount middleware functions to run on incoming HTTP requests, before you handle those requests. It will mount these functions to the specified routes.

In this example you specified the route “/name”, so it will run on requests to that route only.

In this request, no route is specified, so it will run on all incoming HTTP requests.

bodyParser object provides you with various functions to perform actions on the body of HTTP requests. It will populate the results to req.body.

bodyParser.json() will convert a body element that is in the form of JSON into a Javascript object.

bodyParser.urlEncoded() is for body elements that have been URL encoded. The argument { extended: false } is the list of “options”. exended: false specifies the “standard” library of URL encoding values. Setting it to “true” uses a more advanced library that honestly, I don’t know anything about. This middlewhere will only run on requests that have the “type” that is set to the one given in the list of options. Since no type is specified, it uses the default type: application/x-www-form-urlencoded.

Read more about using middlewhere here and bodyParser here, and HTTP requests here.

Hope that helps :slight_smile:

it does. Thanks fo your help :slight_smile:

1 Like