From c25d025e57a3e8fa2daed56f7321e3ab1a6f763a Mon Sep 17 00:00:00 2001 From: festus-sulumeti Date: Sat, 29 Jun 2024 22:30:30 +0300 Subject: [PATCH] Add error handling, logging, and request validation --- listener.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/listener.js b/listener.js index 73efba5..ee95965 100644 --- a/listener.js +++ b/listener.js @@ -7,10 +7,26 @@ const options = { const express = require('express'), bodyParser = require('body-parser'), app = express(); +const morgan = require('morgan'); +const { check, validationResult } = require('express-validator'); +app.use(morgan('combined')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); +// Middleware to handle validation errors +const validateRequest = [ + check('transactionID').notEmpty().withMessage('transactionID is required'), + check('amount').isNumeric().withMessage('amount must be a number'), + (req, res, next) => { + const errors = validationResult(req); + if (!errors.isEmpty()) { + return res.status(400).json({ errors: errors.array() }); + } + next(); + } +]; + /* B2C ResultURL URL: /b2c/result