So one of my requirements is that ALL responses must be of the following structure.{ success: true/false data: {}/[] } This even has to be done with any errors I send. No problem, expect...I have around 50 endpoints I need to make, so my res.json will be littered withres.json({success: true, data: someDatabaseResponse}); So I will end up adding this to every single route I have...mmhmm okay so I make a class instead.class ServerResponse { constructor(data, success = true) { this.data = data; this.success = success; } } module.exports = ServerResponse; This works expect...well same thing, every res.json will have a new ServerResponse class insteadres.json(new ServerResponse(dataResponse, true)); Maybe using middlewares? Well, then I will have to pass the success and true to the next method.next(dataResponse, true?) Either way I don't really want to be using next() anyways. So I ask you, is there a way in express to make sure all JSON responses have the same properties I want without defining it in every res.json method?
Submitted August 21, 2019 at 07:10PM by HappyZombies
No comments:
Post a Comment