Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

--How to implement req.body.param validation

Status
Not open for further replies.

JPSeo

Systems Engineer
Sep 3, 2020
6
US
Hello:

am trying to validate the value of MAXDEPTH below. if it is greater than x or less than y, execute the function updateMaxdepth(). Otherwise print error saying input value out of bounds.

So I have inserted the if/else block below but its not working.
It is at the borrom of the following code.
Pls advise what I have missed. Thank you.
=======================================================
Code:
app.post('/resubmit', urlencodedParser, function(req, res) {

    var QMGR      = req.body.qmgr;
    var APP_ID    = req.body.app_id;
    var QUEUE     = req.body.queue;
    var CURDEPTH  = parseInt(req.body.curdepth);
    var MAXDEPTH  = parseInt(req.body.maxdepth);

    var bash =
{
    cmd_1: `su mqm -c dspmq | grep ${QMGR}`,
    cmd_2: `su mqm -c 'echo \"dis queue(${QUEUE}) CURDEPTH MAXDEPTH\" | runmqsc ${QMGR}' | grep MAX |grep -v dis| perl -lanF\"[()]\" -e 'print $F[1]'`,
    cmd_3: `su mqm -c 'echo \"dis queue(${QUEUE}) CURDEPTH MAXDEPTH\" | runmqsc ${QMGR}' | grep MAX |grep -v dis| perl -lanF\"[()]\" -e 'print $F[3]'`,
    cmd_4: `su mqm -c 'echo \"alt ql(${QUEUE}) maxdepth(${MAXDEPTH})\" | runmqsc ${QMGR}'`
}

    let transport = nodemailer.createTransport({
    host: 'smtp.qcorpaa.aa.com',
    port: 25,
    //port: 587,
    secure: false,
    tls: {rejectUnauthorized: false}
    //tls: { ciphers:'SSLv3' }
});

    console.log(req.body.qmgr+" "+req.body.app_id+" "+req.body.queue+" "+req.body.curdepth+" "+req.body.maxdepth);

function updateMaxdepth() {
  //return new Promise((resolve) => {
    var child = cp.exec(bash.cmd_4, function(err, stdout, stderr) {
    if (stdout.includes('changed')) {

    var mailOptions = {
    from: 'root@esmqst15',
    to: 'JP.Seo@aa.com',
    //to: 'DL_Enterprise_MQ@aa.com',
    subject: 'Maxdepth Updated for queue '+req.body.queue+' on QMGR '+req.body.qmgr,
    text: 'Maxdepth Updated for queue '+req.body.queue+' to '+req.body.maxdepth+' on QMGR '+req.body.qmgr+' by App_ID '+req.body.app_id
    };
    console.log(mailOptions);
    transport.sendMail(mailOptions, function(err, info) {
    if (err) {
      console.log(err)
    } else {
      console.log(info);
    }
    });
    }
    });
}

function getCurDepth() {
  return new Promise((resolve) => {
    var child = cp.exec(bash.cmd_2, function(error, stdout, stderr) {
      let curr_output = stdout.split(/[\r\n|\n|\r]/).filter(String);
      resolve(curr_output);
    });
  });
}

function getMaxDepth() {
  return new Promise((resolve) => {
    var child = cp.exec(bash.cmd_3, function(error, stdout, stderr) {
      let max_output = stdout.split(/[\r\n|\n|\r]/).filter(String);
      resolve(max_output);
    });
  });
}

Promise.all([getCurDepth(), getMaxDepth()]).then(([curDepth, maxDepth]) => {
  console.log(`CURDEPTH  : ${QMGR}_${APP_ID}_${QUEUE}: ${curDepth}`);
  console.log(`MAXQDEPTH : ${QMGR}_${APP_ID}_${QUEUE}: ${maxDepth}`);

  //if(req.body.maxdepth > 1000 ||  req.body.maxdepth < 100000) {
[b]  if(maxDepth > 1000 || maxDepth < 100000) {
  updateMaxdepth();
  res.render("resubmit", {
      data: {
           qmgr     : QMGR,
           app_id   : APP_ID,
           queue    : QUEUE,
           curdepth : curDepth,
           maxqdepth: maxDepth
            }
      });
  } else {
    return res.status(400).json({msg:'not valid'});
  }[/b]
  });

});
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top