Reputation: 11
I am using Express 4.11.1, and Body-parser 1.11.0. When I run the following code I am getting following output
Please suggest how to get the form value
Output
{}
server.js
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var router = express.Router();
app.use(bodyParser.json(),bodyParser.urlencoded({ extended: true }));
// Routes
app.get('/', function(req, res){
res.sendFile(__dirname + '/index1.html');
});
app.post('/', function(req, res){
console.log(req.body);
res.send(req.body);
res.sendFile(__dirname + '/index1.html');
});
app.listen(3000,function(){
console.log("Working on port 3000");
});
index1.html
<!doctype html>
<html>
<body>
<form id="frmTest" name="frmTest" action="http://localhost:3000/" method="post">
<input type="text" id="mytext" value="sadfsad fsd fsad" />
<input type="submit" id="mysubmit" />
</form>
</body>
</html>
Upvotes: 1
Views: 3861
Reputation: 167
app.post('/', function(req, res){
console.log(req.body.**NAMEATTRIBUTE**);
res.send(req.body);
res.sendFile(__dirname + '/index1.html');
});
Give your html 'name' attribute.
Upvotes: 0
Reputation: 7438
To retrieve data on backend side you must just add name to input field
<!doctype html>
<html>
<body>
<form id="frmTest" name="frmTest" action="http://localhost:3000/" method="post">
<input type="text" id="mytext" name="mytext" value="sadfsad fsd fsad" />
<input type="submit" id="mysubmit" />
</form>
</body>
</html>
Upvotes: 1