Skip to content

Instantly share code, notes, and snippets.

@sahilkashyap64
Created June 17, 2023 10:33
Show Gist options
  • Save sahilkashyap64/d912a540a94f7c1c6a004c87bada4b9b to your computer and use it in GitHub Desktop.
Save sahilkashyap64/d912a540a94f7c1c6a004c87bada4b9b to your computer and use it in GitHub Desktop.
chaninging middleware nodejs
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