Monday, 1 May 2017

POST not working on Node.JS & Express

I'm trying to get user input from a html form but can't retrieve the data. This is the HTML code:

<form action="/login" method="POST" enctype="application/x-www-form-urlencoded">
    <div class="form-group">
        <label for="username">Username</label>
        <input type="text" class="form-control" id="username" placeholder="Username">
    </div>
    <div class="form-group">
        <label for="password">Password</label>
        <input type="password" class="form-control" id="password" placeholder="Password">
    </div>
    <button type="submit" class="btn btn-default">Login</button>
</form>

And here is the Node.JS code:

var bodyParser = require('body-parser'); 

var express = require('express');

var app = express();

app.use(bodyParser.json());

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

When I input data on the form, the post request returns {}. However, using Postman it works just fine...



via Ivan

No comments:

Post a Comment