nodejs form post request is not working

2.8k views Asked by At

I working with nodejs and jade to create a login form. In my first step i want to pass the fieldname (id=name, and name=name) to the backend. I have tried both methods (sending post request from form and from postman) but either methods give no response the console on by requesting the fieldname in the backend. What i do see is that the router opens the right function. The code below shows the scenario:

router.js

app.post('/login/post', require('./views/login/index').login);

login.js

exports.login = function (req, res, next){
//This works
console.info('Login...')
//This does not work
console.info(req.body.name)
//This works
res.render('login/index')}

login.jade

    form(method='post', action='/login/post', enctype='multipart/form-data')
      input#name.form-control(type='text', name='name')
      button#submit.btn.btn-primary.btn-lg.btn-block.login-button(type='submit') Login

APP.JS

//dependencies
var config = require('./config'),
    express = require('express'),
    cookieParser = require('cookie-parser'),
    bodyParser = require('body-parser'),
    session = require('express-session'),
    mongoStore = require('connect-mongo')(session),
    http = require('http'),
    path = require('path'),
    multer  =   require('multer'),
    passport = require('passport'),
    mongoose = require('mongoose'),
    helmet = require('helmet');


var storage =   multer.diskStorage({
    destination: function (req, file, callback) {
        callback(null, './uploads');
    },
    filename: function (req, file, callback) {
        callback(null, file.fieldname + '-' + Date.now());
    }
});

var upload = multer({ storage : storage}).single('userPhoto');

 //   csrf = require('csurf');

//create express app
var app = express();

//keep reference to config
app.config = config;

//setup the web server
app.server = http.createServer(app);

//setup mongoose
app.db = mongoose.createConnection(config.mongodb.uri);
app.db.on('error', console.error.bind(console, 'mongoose connection error: '));
app.db.once('open', function () {
});

//config data models
require('./models')(app, mongoose);

//settings
app.disable('x-powered-by');
app.set('port', config.port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

//middleware
app.use(require('morgan')('dev'));
app.use(require('compression')());
app.use(require('serve-static')(path.join(__dirname, 'public')));
app.use(require('method-override')());
app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser(config.cryptoKey));
app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: config.cryptoKey,
  store: new mongoStore({ url: config.mongodb.uri })
}));
app.use(passport.initialize());
app.use(passport.session());
//app.use(csrf({ cookie: { signed: true } }));
helmet(app);

//response locals
app.use(function(req, res, next) {
 // res.cookie('_csrfToken', req.csrfToken());
  res.locals.user = {};
  res.locals.user.defaultReturnUrl = req.user && req.user.defaultReturnUrl();
  res.locals.user.username = req.user && req.user.username;
  next();
});

//global locals
app.locals.projectName = app.config.projectName;
app.locals.copyrightYear = new Date().getFullYear();
app.locals.copyrightName = app.config.companyName;
app.locals.cacheBreaker = 'br34k-01';

//setup passport
require('./passport')(app, passport);

//setup routes
require('./routes')(app, passport);

//custom (friendly) error handler
app.use(require('./views/http/index').http500);

//setup utilities
app.utility = {};
app.utility.sendmail = require('./util/sendmail');
app.utility.slugify = require('./util/slugify');
app.utility.workflow = require('./util/workflow');

//listen up
app.server.listen(app.config.port, function(){
  //and... we're live
  console.log('Server is running on port ' + config.port);
});

Please can you help me with this problem.

1

There are 1 answers

0
Arpit Solanki On BEST ANSWER

multipart/form-data is not supported by bodyparser so you have to either use a middleware like multer for the support of form-data or you can change the content-type to x-www-form-urlencoded. This post would be helpful to you.