Skip to content
Advertisement

Simple Tutorial code not getting data from a Login form (express js)

So I am following a YouTube Tutorial on how to set up a simple login server (channel name: web dev simplified, video: “Node.js Passport Login System Tutorial”)

The code (at the stage I am at @ 14:12 mark in video) is suppose to console-log an array of a new registered user at the terminal.

but I get an empty array instead. Despite having the same code (I followed him step by step) I do not get the new user. What am I doing wrong?

const path = require('path');
const fs = require('fs');
const express = require('express');
const app= express();
const bcrypt = require('bcrypt');
const session = require('express-session');
// Get express ready to setup server
var users =[];
//{id:"", name:"", email:"", password:"",}



app.set('view-engine', 'ejs')
app.use(express.urlencoded({ extended: false }));

// Sets up main page
app.get('/', (req, res) => {
 res.render('index.ejs', {name: 'Moe'})   
})

////////////////Sets up login page////////////////////////
app.get('/login', (req, res) => {
    res.render('login.ejs')   
   })
///////////////Sets up register page////////////////////////
app.get('/register', (req, res) => {
    res.render('register.ejs')   
   })
///////////////////// Recieves date from register /////////////////
app.post('/register',  async (req, res) => {
    try {
      const hashedPassword = await bcrypt.hash(req.body.password, 10)
      
      users.push({
        id: Date.now().toString(),
        name: req.body.name,
        email: req.body.email,
        password: hashedPassword
      })
      res.redirect('/login')
    } catch {
      res.redirect('/register')
    }
    console.log(users);
  });




///////////////////////////////////////////////////////




// Setup Server
app.listen(5000);

html for login

<h1>Login</h1>

<form action="register" method="post">
<div>
<label for="name">username</label>
<input type="text" id="name" name="name" required>
</div>
<br>
<div>
<label for="name">password</label>
<input type="text" id="password" password="password"required>
</div>
<br>
<button type="submit">Login</button>
<br>
<a href="register">Register</a>
</form>
<br>
back to <a href="/">Homepage</a>

html for register

<h1>register</h1>

<form action="register" method="POST">
<div>
<label for="name">Name</label>
<input type="text" id="name" name="name" required>
</div>
<br>
<div>
<label for="email">email</label>
<input type="email" id="email" name="email" required>
</div>
<br>
<div>
<label for="password">password</label>
<input type="password" id="password" password="password"required>
</div>
<br>
<button type="submit">Register</button>
<br>
<a href="login">Login</a>




</form>
<br>
back to <a href="/">Homepage</a>

Advertisement

Answer

The request body needs to be parsed. Add this above your routes: app.use(express.json()); ^^^ Scratch this part. It’s only needed with a JSON payload ^^^

In your html you have password=”password” instead of name=”password” for the password input element.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement