Express JsNodejs

Nodejs express routes with express validatiors

Nodejs express routes with express validatiors

Hello to all, welcome to therichpost.com. In this post, I will tell you, Nodejs express routes with express validatiors.

Express Validatior

Post Working:

Friends in this post, I using express validatior middleware into my nodejs application on express routes. I personally like this very much because security reasons.

Here are some working validation checks images with postman:


Here is the working code snippet for Nodejs express routes with express validatiors and please follow carefully:

1. Firstly, we need to create empty folder name whatever you like but I named it “therichpostnode” and after it, create empty file name “node.js” file in that folder and add below code inside that file:

//Express mainly uses for routing
const express = require('express');
const app = express();

//Cors modules used for fix cors issue in routing and get and post data
const cors = require('cors');
app.use(cors());

//Express Validator for routing validation middleware
const { check, validationResult } = require('express-validator');

// create application/json parser
const bodyParser  = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));


//Post request with express validator check on email and password
app.post('/user', [
    // username must be an email
    check('username').isEmail().withMessage("Email is not valid"),
    // password must be at least 5 chars long
    check('password').isLength({ min: 5 }).withMessage("Password should be 5 chars long")
  ], (req, res) => {
    
    //console.log(req);
    // Finds the validation errors in this request and wraps them in an object with handy functions
    const errors = validationResult(req);
    
    if (!errors.isEmpty()) {
        console.log(errors);
      return res.status(400).json({ errors: errors.array() });
    }
  
    res.send("Everything is working fine..");
});

app.listen(8080, () => {
console.log('Server started!');
});

 

2. After added the above code into your node.js file please run below commands into your project terminal to get the dependent modules:

npm install cors --save

npm install express-validator

npm i express

npm i body-parser 

//in the end please run below command

node node.js

 

 

This is it friends and if you have any kind of query then please do comment below and you can also share your views on this.

Friends, we can use this Express-Validator in many ways and here they are:

  • Nodejs form validation
  • Angular serve side validation with express routing
  • Reactjs server side validation with express routing

Jassa

Thanks

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, Angular 8, Angular 9, Angular 10 Nodejs, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.