0

I'm trying to get the username and password from a form:

<!DOCTYPE html>

<form action="" method="post" enctype="application/x-www-form-urlencoded">  
  Username:<br>
  <input type="text" name="Username" value="Username">
  <br>
  Password:<br>
  <input type="text" name="Password" value="Password">
  <br><br>
  <input type="submit" value="Submit">
</form> 

This is then received by this:

app.post('/login', function(req, res){
console.log(req.body.username);
console.log(req.body.password);
});

However no matter what enctype I put in, the username and password is always undefined. What is the error?

P.S Was using body-parser

  • I still get undefined for username and password –  Mar 02 '17 at 15:18
  • Possible duplicate of [What does body-parser do with express in nodejs?](http://stackoverflow.com/questions/38306569/what-does-body-parser-do-with-express-in-nodejs) - I'm guessing you haven't installed body-parser and called `app.use(bodyParser.urlencoded())` in your app. – Joe Clay Mar 02 '17 at 15:19
  • Also, you might want to use placeholder instead of value on your inputs, and maybe mark them 'required'. `` – rasmeister Mar 02 '17 at 15:20
  • I have installed body parser and i've included app.use(bodyParser.urlencoded({extended: true})); –  Mar 02 '17 at 15:22
  • Call `console.log(req.body)` and search needed fields inside. – NechiK Mar 02 '17 at 15:25

1 Answers1

0

Try

console.log(req.body.Username);
console.log(req.body.Password);
rasmeister
  • 1,986
  • 1
  • 13
  • 19