Madasamy
Madasamy

Reputation: 67

How to do form field validation in node.js ?(express-validator)

In My package.json files

  "express": "~4.9.0",
  "express-validator": "~2.7.0"
   .
   .
   . 

In my app.js file

var expressValidator = require('express-validator');
.
.
.
app.use(expressValidator);

When i go to browser, the respective page is not displayed? It is continuously loading. After remove express validator relevant code, the page render properly.

Upvotes: 0

Views: 831

Answers (2)

agm1984
agm1984

Reputation: 17178

Check if you have bodyParser installed.

Express-validator recommends that it is called immediately after bodyParser in app.js.

Example:

 // bodyParser
 app.use(bodyParser.json());
 app.use(bodyParser.urlencoded({ extended: true }));

 // Express-validator
 app.use(expressValidator({
   errorFormatter: function(param, msg, value) {
       var   namespace = param.split('.'),
             root      = namespace.shift(),
             formParam = root;

     while(namespace.length) {
       formParam += '[' + namespace.shift() + ']';
     }
     return {
       param : formParam,
       msg   : msg,
       value : value
     };
   }
 }));

Upvotes: 1

zeekrey
zeekrey

Reputation: 451

Unfortunately this is not much code to work with. But you could try it with:

app.use(expressValidator());

instead of

app.use(expressValidator);

Upvotes: 0

Related Questions