How to validate and handle a form in Express (NodeJS) How to validate and handle a form in Express (NodeJS) express express

How to validate and handle a form in Express (NodeJS)


It looks like there's a module for this located at https://github.com/caolan/forms. I've never used it, but it seems fairly full featured.


This also looks viable and is still being developed: https://github.com/ctavan/express-validator

Here's an example of validating a form submission (login post request):

exports.login.post = function(req, res){  req.assert('username', 'Enter username').notEmpty();  req.assert('password', 'Enter password').notEmpty();  res.locals.err = req.validationErrors(true);  if ( res.locals.err ) {    if ( req.xhr ) {      res.send(401, { err: res.locals.err });    } else {      res.render('login', { err: res.locals.err });    }    return;  } //authenticate user, data is valid};