Step 5

Open views/registeration.njk and notice the user registration form:

<form action="/register" method="POST">
  <input type="text" name="username" placeholder="username" class="field">
  <input type="password" name="password" placeholder="password" class="field">
  <input type="submit" value="Register" class="btn">
</form>

In particular notice the action and method attributes for form element. As a client provides their desired username and password, and clicks on "Register" button, the "form" will send a POST HTTP request along with the "username" and "password" data.

Open the index.js and focus on the logic for handling POST request through /register endpoint:

app.post("/register", (req, res) => {
  const username = req.body.username;
  const password = req.body.password;
  console.log(colors.cyan("Register:", { username, password }));
  // TODO register the user!

  // redirect to the login page
  res.redirect("/");
});

Notice we receive the "username and "password" from req.body. This is not possible by default unless you use the following middleware (as it is done in index.js):

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

The express.urlencoded() function is a built-in middleware in Express. We use it in order to parse the body parameter of request that was sent from a "HTML form" on the client application. The extended: false prevents parsing "nested object" (considered a good practice).

Notice I also use the colors package that allows us to get colors in your NodeJS console. This is not needed; I've just added it here for fun!

console.log(colors.cyan("Register:", { username, password }));