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 trial

JavaScript Express Basics Deeper into Routing with Express The Request Object

Don't use body-parser

This course needs updating. You do not need to install anything to get the body with Express, at least not any more. You can use Express's own middleware to do this:

// middleware for parsing http payloads
app.use(express.urlencoded());

app.all('/hello', (req, res) => {
    console.log(req.body)
    res.render('hello', { name: req.body.username });
});

Also, since the /hello route uses both GET and POST it makes a lot more sense to use Express's express.all method which responds to all http verbs rather than having two separate routes for GET and POST. Again, update the course please.

Steven Parker
Steven Parker
230,230 Points

You may also want to make your suggestion directly to the Support staff.

Thanks, good call.

Alex Vien
Alex Vien
7,615 Points

What is the purpose of the following parameter in res.render?

{ name: req.body.username }

7 Answers

It should be noted that in this video he explicitly has us install "express": "^4.15.2",

The express docs for express.urlencoded([options]) state: "This middleware is available in Express v4.16.0 onwards."

So either way, use the middleware from the video, or make sure to update your express version before trying this suggestion

I'm glad I read this before investing my time on this. Thanks!

Thanks for this - is someone able to explain in simple terms that the difference is between extended "false" and "true" and when you'd use it?

I was just reading the express docs. As of Jun-19-2020 express 5.x is still in the early stages of testing and isn't fully released yet. It's only in alpha mode. Your comments are appropriate for 5.x but personally, for now, I'd prefer following how Andrew Chalkley has laid out the lesson.

I might be late but express.urlencoded() and get.all() are both part of express 4.x now