Reputation: 23
login.html(I have converted it to jade) is my login page which is opened at localhost:3000. I send the form details to index.js(server).
Problem: On console.log(username) I get output as undefined. Please help. Have I used body-parser correctly ?
var express= require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var bodyParser = require('body-parser');
app.use(express.static('./Public'));
app.set('views', __dirname + '/tpl');
app.set('view engine', "jade");
app.engine('jade', require('jade').__express);
app.get("/", function(req, res){
res.render("login");
});
app.use(bodyParser.urlencoded({
extended : false
}));
app.use(bodyParser.json());
app.post('/', function(req,res) {
var username = req.body.name;
console.log(username);
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
html code: (login.html)
<html>
<body>
<form action="/" method="post" enctype="multipart/form-data">
<fieldset>
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter your full name" />
<br />
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter your email address" />
<br />
<input type="submit" value="Create Profile" />
</fieldset>
</form>
<script>
var socket = io.connect("http://loacalhost:3000");
</script>
</body>
</html>
Upvotes: 2
Views: 969
Reputation: 5708
If you look documentation of body-parser you can notice this:
This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:
busboy and connect-busboy multiparty and connect-multiparty formidable multer
So you have to either change enctype="multipart/form-data" to for example enctype="application/json" or to use some other module.
Hope that I have helped you.
Upvotes: 2