
Express JS Body Validation using Express Validator and Custom Methods
Written : 2022-06-12 Last Update : 2022-06-12
Express Js Knowledge Required
Body Validation
Client side validation is not enough and it may be manipulated to send harmful data to server even with front end validation. Which is why it is important to make sure that we have some kind of server side validation too.
Setting Up our App
We need to just install express js, setup a basic app with json body parser.
const express = require('express');
const app = express();
app.use(express.json());
app.get('/', (req, res) => {
res.send("Welcome");
});
const PORT = process.env.PORT || 8000;
app.listen(PORT);
Custom Validation
Express Validator
Express Validator supports both javascript and typescript. It also supports Sanitization.
Sanitization
Sometimes, receiving input in a HTTP request isn't only about making sure that the data is in the right format, but also that it is free of noise.
Setup
npm install express-validator