Thursday 27 December 2018

Is it inadvisable to export request handlers as objects?

My first post here, thanks for the help in advance.So I am working on the backend of a product that uses node+express and was developed as part of a hackathon, much before I joined the team. The code was riddled with malpractices and pitfalls, which is why I was tasked with refactoring the code to meet enterprise standards (It is primarily a B2B app).One of the conscious decisions I took to make the code look cleaner and more modular is have the routers (like app.get('endpoint'), (req,res)=>{...}) in a different file and the actual request handler functions (like function createEntity(req, res)) in a different file, grouped based on what virtual data entity they operate on. I am doing request validation in the router and calling the request handlers only when request has been validated.The thing is, I am exporting the request handler functions as key-value pairs in a JSON object like so :module.exports = {createEntity: createEntity,getEntity: getEntity};I just had a round of code review by my seniors and one them, having about 7 months more experience than me in the team, raised the issue that what I am doing is unwittingly implementing a "Singleton" pattern, which I know node does by default, but according to this article should be done only knowingly. My manager says his point "makes complete sense".My question is, do we have to put an effort into not exporting objects in case of request handlers which are bound to a specific path in the API and won't sensibly be required (imported) in more than one place? If yes, what would exporting a constructor look like in this scenario.Edit: grammar

Submitted December 28, 2018 at 04:28AM by enkayjee

No comments:

Post a Comment