Question App in Nodejs, Pug, Body-Parser Parsing Form Data 4

Lesson Summary

Our question app in Nodejs is coming along nicely. Slowly but surely we will make it happen. In today’s lesson we look at reading form data. The first thing we do is create a GET route (‘/hello’) which renders a new file we create called ‘hello.pug’. Our ‘hello.pug’ file welcomes a user only if he has entered his name? We do this using an if statement. We say if name exists welcome user with “Hello #{name}” We have an else right after which will run if no name exists which displays a form which asks user to enter their name. In order to read in their name entered in the form we learn that we need to parse their form data. We ‘npm install body-parser’ which parses form data and sends it in the request. We access the value which will be in the ‘body’ of the request. In our route we target our form data, which we gave the name ‘username’ to, by using ‘req.body.username’. This lesson is vital in your journey towards backend development, for it allows you to parse valuable form data. Next we will learn how to save our sessions using cookies.

Subscribe To Monthly Newsletter. Chance to Win Monthly Prizes

* indicates required