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

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

Outdated body-parser? Returns #{name}

When using the mentioned bodyParser i am returned with a message saying: "body-parser deprecated undefined extended: provide extended option"

I googled a bit and found on reddit to use something else.

https://stackoverflow.com/questions/25471856/express-throws-error-as-body-parser-deprecated-undefined-extended

app.use(express.urlencoded({ extended: true })) post may

However it still returns the message Hello #{name}, without the value, but just the template literal.

app.js

const express = require('express');
const app = express();



app.set('view engine', 'pug');

app.get('/', (req, res) => {


    res.render('index')
    res.send("<h1> yayyy connection! </h1>");

});



app.get('/hello', (req, res) => {
    res.render('hello');

});

app.post('/hello', (req, res) => {

    res.render('hello', { name: req.body.username });

});

app.listen(3000, () => {
    console.log("The app is on the following local host: 3000 :)");


});

hello.pug

extends layout

block content
    if name
        h2 Hello {#name}
    else
        form(action='/hello', method='post')
            label What is your name?
                input(type='text', name ='username')
            button(type=submit) Submit
Mark Westerweel
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Mark Westerweel
Full Stack JavaScript Techdegree Graduate 22,378 Points

You are probably on a newer version of Express (4.16.0 and up). You don't need body-parser anymore as suggested by this user.

You can check the version of Express (and others) by npm list in the terminal

1 Answer

James Crosslin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,882 Points

This is an old one, but the issue is in your hello.pug

        h2 Hello {#name}

should be

        h2 Hello #{name}

Everyone should definitely be using the following instead of body-parser.

app.use(express.urlencoded({ extended: true }))

Check out the express documentation for more info. https://expressjs.com/en/api.html#req.body