How to implement interfaces in NodeJs/Express-Structure

I have generated a new NodeJs/Express.Project via:

npm install -g express-generator

..which generated me the folder-structure

  • /bin
  • /public
  • /routes
  • /views

.. now I want to make some interfaces like

interface options = {
 foo: String,
 bar: String

where would be the best place to store those and how would I implement/include them into my js-files?

1 answer

  • answered 2018-11-08 07:55 Nelson Owalo

    Javascript/nodejs has no notion of interfaces, not really. JavaScript is a dynamic language, one where types are changed so often that the developer may not have even realised, because of this people argue there is no need for an interface to be added to the ECMAScript standard that JavaScript is based on.

    You could use this SO as a reference and come up with some implementation, but you will find it hard to enforce in your code:

    The simplest approach you can try is to define the contract informally (comments or documentation - maybe) and simply rely on the developers at each side of the interface to know what they are doing.

    This is called "duck typing" —if it walks like a duck and it quacks like a duck, then it is a duck.

    Though lets be honest, in javascript, an objects type in itself is not important