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 Response Object

Cannot see the welcome message after submitting the name

After I added the app.post method in the app.js file, I couldn't see the appropriate results. So I'm basically seeing the "Flash Cards" in the header, the label, the form, the button and the message in the footer. As far as I can see my code is the same as the instructor's. Any ideas about why this might be happening? Here's my code:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.urlencoded({extended: false}));

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

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

app.get('/cards', (req, res) => {
    res.render('card', {prompt: "Who is burried in Grant's tomb?"});

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

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

app.listen(3000, () => {
    console.log('The application is running on localhost:3000!');

This is the code from my hello.pug file:

extends layout.pug

block content
    if name
        h2 Welcome #{name}!
        form(action='/hello', method='post')
            label Please type your name:
                input(type='text', name='username')
            button(type='submit') Submit

I don't know if it's relevant, but I'm also getting the following line on my command prompt every time I hit the submit button:

[Object: null prototype] { username: 'Stella' }

1 Answer

Hello Stella,

Looking at your code, my guess would be that when you are submitting your name, you are in the "else"-branch of your hello.pug template. After you click submit, the username isn't stored anywhere. HTTP requests (such as the 'post'-method) are stateless and not remembered by the server. Therefore, the if-statement in your hello.pug template will never be true (the name value does not exist) and thus the Welcome message is never shown.

In the upcoming videos Andrew explains a bit more about this concept and how we use cookies in order for servers to remember the input value.

Good luck!