javascript - Expressjs module bodyparser returns undefined -
i playing around simple html form, trying parse form data. unfortunately routes section returns undefined form elements
my app.js
var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var busboy = require('connect-busboy'); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); // uncomment after placing favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: true })); app.use(cookieparser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); app.use('/users', users); module.exports = app;
jade form,
form(method='post', action='/contact', enctype="multipart/form-data") .form-group label name: input.form-control(name='name', type='text') input.btn.btn-default(name='submit',type='submit',value='save')
my route,
router.post('/contact', function(req, res, next) { var name = req.body.name; console.log(req.body.name); res.redirect('google.com'); });
your form has multipart/form-data
enctype:
form(method='post', action='/contact', enctype="multipart/form-data")
but written here, body-parser
doesn`t handle such type of bodies:
this not handle multipart bodies, due complex , typically large nature. multipart bodies, may interested in following modules:
or should change middleware parse body, or form enctype
.
Comments
Post a Comment