1

I have several functions but I dont know how collect user defined functions in liberary to reuse them in all around the nodeJs projec?

EramSa
  • 128
  • 1
  • 10

1 Answers1

1

you could do something like this: In your preferred folder, create a file like user.js Then, define a class, E6 way :

class Users {
//if you need a constructor, but it's not mandatory
constructor(username,email,otherParamYouNeed){
this.username = username;
this.email = email;
this.otherParamYouNeed = otherYouNeed
}

//then organize your user functions there

userFunctionThatDoesSomething(){
//do what you need
}
userFunctionThatDoesAnotherThing(){
 // do what you need
}
}

//then export the class
module.exports = {Users}

After, in the file in which you need to call those functions :

var {Users} = require ('/path/to/user.js');
//if you have constructor in your class
var user = new Users(username,email,otherParamYouNeed);
//if not
var user = new Users;

After that, you will be able to use the functions you declared in the class in the file you have required the class in, like :

user.userFunctionThatDoesSomething(etc..);

Have a look at https://www.sitepoint.com/object-oriented-javascript-deep-dive-es6-classes/