Created
June 17, 2023 10:33
-
-
Save sahilkashyap64/d912a540a94f7c1c6a004c87bada4b9b to your computer and use it in GitHub Desktop.
chaninging middleware nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function prepareForMiddleware(req,res,next,data){ | |
res.return=data; | |
next(); | |
} | |
function productDetails(){ | |
// return res.json({ message: 'Product Details', product, similar, variants }); | |
let data = { message: 'Product Details', product, similar, variants }; | |
prepareForMiddleware(req,res,next,data); | |
} | |
//midlleware | |
function extraQuantity(req, res, next) { | |
if (res.hasOwnProperty('return')) { | |
// Add extra quanity | |
if(res.return.hasOwnProperty('data')){ | |
res.return.data.map(function(i) { | |
i.Qty = i.Qty + config.dev.allowedExtraQuantity; | |
return i; | |
}); | |
} | |
if(res.return.hasOwnProperty('product')){ | |
const newObject = {...res.return.product.toObject(), Qty : res.return.product.Qty+config.dev.allowedExtraQuantity} | |
res.return.product=newObject; | |
} | |
if(res.return.hasOwnProperty('similar')){ | |
res.return.similar.map(function(i) { | |
i.Qty = i.Qty + config.dev.allowedExtraQuantity; | |
return i; | |
}); | |
} | |
return res.send(res.return); | |
} | |
} | |
//routes | |
Router.route(base + '/:id') | |
.get(productDetails,extraQuantity) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment