samedi 22 janvier 2022

exporting function vs const then export

I'm a NOOB to NodeJs and trying to understand the difference and pros and cons to a couple of patterns ....

In a controller I often see people use :

exports.myFunction = async function (req, res) { ......}

In other examples I see

const myFunction = async function (req, res) { ......}
module.exports = {myfunction}
  1. what is the advantage of declaring the function as constant then exporting it vs exporting it as part of the definition? Is it just style, or will it make the code more testable or something else?

  2. I have seen that people code services as classes and export the whole class. Is this best practice?

  3. Why not define controllers as classes, and why not export the class?

I see these patterns mixed up in various articles talking about patterns and practices and haven't got my head around the nuances.

Thanks.

Aucun commentaire:

Enregistrer un commentaire