0

When submit the form the form fields value not came in post method.

here is code in index.html file

<form method="POST" action="http://localhost:4000/insertion">
    <div class="form-group">
        <label>E-Mail</label>
        <input type="text" name="email" class="form-control" >
    </div>
    <div class="form-group">
        <label>Password</label>
        <input type="password" name="password" class="form-control" >
    </div>
    <div class="form-group">
        <label>&nbsp;</label>
        <input type="submit" name="submit" class="btn btn-primary" value="Submit">
    </div>
</form>

in server side script code is

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

var app = express();

app.use(bodyParser.json())

app.use(cors());

app.post('/insertion',function(request,response){
    console.log(request.body);
});

app.get('/index',function(request,response){
    response.send('hi ');
});

app.listen(4000);

its not showing showing any error but value is empty

manikandan
  • 677
  • 2
  • 8
  • 19

3 Answers3

1

You need use bodyParser.urlencoded to read the form data.

Add this and try again.

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
Naren
  • 4,152
  • 3
  • 17
  • 28
0

You need to add app.use(bodyParser.urlencoded({extended: false})) after the app.use(bodyParser.json()). You can see documentation here

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

var app = express();

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))

app.use(cors());

app.post('/insertion',function(request,response){
    console.log(request.body);
});

app.get('/index',function(request,response){
    response.send('hi ');
});

app.listen(4000);
Samuel Chibuike
  • 146
  • 2
  • 4
0

follow this... need to pass value in url function

var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.post('/formdata',urlencodedParser,function(req,res){
    console.log(req.body);
});
manikandan
  • 677
  • 2
  • 8
  • 19