Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrendan Moran
14,052 Pointsreq.body.email -- does the email property come from the name attribute?
Just like the title of the question says. Just a quick look at the jade template says that it does come from the name attribute. But why?
Where can I find this documented? Does this behavior come from the node API, or express? Or is this just the way the DOM does it, and the data being based is the whole DOM? But then I would expect that we would be using document.email, document.name, etc. Wondering where the functionality comes from and wanting to dig a little deeper.
1 Answer
andren
28,558 PointsIt's a bit of a combination of multiple of the different things you mentioned. When you submit a form using a post request what is sent is usually an encoded list of name-value pairs, where the name is based on the name attribute of the input and the value is the value of the input.
When that request reaches express it is sent to the body-parser middleware before it reaches your code. Middleware is basically plugins that are used to modify requests that come to express before you process it with your own code in the routes you define.
The body-parser middleware is designed to process form data. When form data is received it parses the data and attaches it to the body element so that you can easily access the key-value pairs without having to do any parsing yourself.
This is not my main area of expertise so I can't give you much detail about exactly how the body-parser parses the data or anything more complex like that, but if you are curious about form submission and the body-parser then I would recommend you Google application/x-www-form-urlencoded
(which is the most common standard for sending form data) and body-parser
if you want more details on how it parses the form data.
Brendan Moran
14,052 PointsBrendan Moran
14,052 PointsThanks! That's an excellent answer. Yeah, I will read up. I think I also need to read more just on how an express app.js works with all the middleware and app.use() calls. I am looking at the error handling in the app.js and I'm freaking mystified by how app.use(function(args){//handle errors here!}) creates an error handler.