node.js - Do i need to validate variables again in router.get method after validating in router.use method? -


i using nodejs , express framework , mysql database

i validating data in use method only

what don't understand should validate data again in method ?

i have code

var express = require('express'); var router = express.router(); var jwt = require('jsonwebtoken');  function isnullorwhitespace(input) { if (typeof input === 'undefined' || input == null) return true; return input.replace(/\s/g, '').length < 1; }  function haswhitespace(s) { return /\s/g.test(s); }    router.use(function(req, res, next) { var auth_key = req.headers['auth_key']; var api_key = req.query['api_key']; var source_id = req.query['source_id']; if( (auth_key) && (api_key) && (source_id) && (auth_key!==undefined) && (api_key!==undefined) &&(source_id!==undefined) && !isnullorwhitespace(auth_key) && !isnullorwhitespace(api_key) && !isnullorwhitespace(source_id) && !haswhitespace(auth_key) && !haswhitespace(api_key) && !(haswhitespace(source_id))) { next(); } else {     if( (!auth_key) || (auth_key===undefined) || isnullorwhitespace(auth_key) || haswhitespace(auth_key))     {         var auth_key_error = {         status : "fail",         message : "invalid auth key"     };           return res.status(403).send(auth_key_error); }     else if((!api_key) || (api_key===undefined) || isnullorwhitespace(api_key) || haswhitespace(api_key))     {         var api_key_error = {             status : "fail",             message : "invalid api key"         };           return res.status(403).send(api_key_error);      }     else if((!source_id) || (source_id===undefined) || isnullorwhitespace(source_id) || haswhitespace(source_id))     {         var sourceid_error = {             status : "fail",             message : "invalid source id"         };           return res.status(403).send(sourceid_error);      }     else{         var fieldsempty_error = {             status : "fail",             message : "some field incorrect or left empty"         };           return res.status(403).send(fieldsempty_error);     } }  });  router.get('/', function (req, res, next) {   var auth_key = req.headers['auth_key']; var api_key = req.query['api_key']; var source_id = req.query['source_id']; //do here      });  module.exports = router; 

now want use header[] , query string [] variables again in method

how should access variables in method properly?

also sanitization ?

do need perform sanitization variables in code ?

also 1 more thing .. read next method used execute next order middleware function

what should if have 1 use function middleware ?

logically data validation might differ 1 route end point another, it's better not use route.use() use methods according verbs using (get, post, delete ...).

use express-validator validate data, gives possibility make async/synchron validation in human readable uniform code.

to access data, usally in req.query, if url sent is: host.com?data1=x ==> req.query.data1

if in body, usally case post/put/patch requests, use req.body.data1.

to access data, have encode them. if sent data in json form, should decode them before accessing them, using middleware purpose, able access them this: req.body.name ....


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -