Spartan Troy
Spartan Troy

Reputation: 1039

how to require a class in node.js

my code that works..

var express=require("express"),
app=express();

class bar {
   constructor()
   {
     this.user_name=null;
     this.user_surname=null;
     this.user_age=null;
   }

   name(user_name) {
     this.user_name=user_name;
     return this;
   }

   surname(user_surname) {
     this.user_surname=user_surname;
     return this;
   }

   age(user_age) {
     this.user_age=user_age;
     return this;
   }

   get(callback) {

     var list ={};

     list.uname=this.user_name;
     list.usurname=this.user_surname;
     list.uage=this.user_age;

     callback(list);
   }

}



app.get("/liste/:ext",function(req,res){

  var ext=req.params.ext;

  res.setHeader('Content-Type', 'application/json');

  if(ext==1)
  {
    var newbar=new bar();
    newbar.name("alex").surname("broox").age(32).get(function(result){
      res.json({data:result})
    })

  }

  if(ext==2)
  {
    var newbar=new bar();
    newbar.name("alex2").get(function(result){
      res.json({data:result})
    })
  }

})

app.listen(4000,function(log){
  console.log("listening")
})

but..following code does not work.. with require that class from other file..

test.js

module.exports = {

class bar {
   constructor()
   {
     this.user_name=null;
     this.user_surname=null;
     this.user_age=null;
   }

   name(user_name) {
     this.user_name=user_name;
     return this;
   }

   surname(user_surname) {
     this.user_surname=user_surname;
     return this;
   }

   age(user_age) {
     this.user_age=user_age;
     return this;
   }

   get(callback) {

     var list ={};

     list.uname=this.user_name;
     list.usurname=this.user_surname;
     list.uage=this.user_age;

     callback(list);
   }

}

};

app.js file.. with require that class

var express=require("express"),
app=express();


require("./test")



app.get("/liste/:ext",function(req,res){

  var ext=req.params.ext;

  res.setHeader('Content-Type', 'application/json');

  if(ext==1)
  {
    var newbar=new bar();
    newbar.name("alex").surname("broox").age(32).get(function(result){
      res.json({data:result})
    })

  }

  if(ext==2)
  {
    newbar.name("alex2").get(function(result){
      res.json({data:result})
    })
  }

})

app.listen(4000,function(log){
  console.log("listening")
})

but why does not it work...please help me..above code that works but this code that does not work..

Upvotes: 6

Views: 29968

Answers (3)

DominicValenciana
DominicValenciana

Reputation: 1731

When exporting classes in node you need to define the class first, then export the class using module.exports followed by the name of the class you wish to export.

// test.js
class Bar {
       constructor() {
         this.user_name=null;
         this.user_surname=null;
         this.user_age=null;
       }

       name(user_name) {
         this.user_name=user_name;
         return this;
       }

       surname(user_surname) {
         this.user_surname=user_surname;
         return this;
       }

       age(user_age) {
         this.user_age=user_age;
         return this;
       }

       get(callback) {

         var list ={};

         list.uname=this.user_name;
         list.usurname=this.user_surname;
         list.uage=this.user_age;

         callback(list);
       }
}

module.exports = Bar

From there you can just require the file and grab the class as such.

var Bar = require('./test');
var bar = new Bar();

Upvotes: 31

Spartan Troy
Spartan Troy

Reputation: 1039

hello again @DominicValenciana

test.js

class bar {
   constructor()
   {
     this.user_name=null;
     this.user_surname=null;
     this.user_age=null;
   }

   name(user_name) {
     this.user_name=user_name;
     return this;
   }

   surname(user_surname) {
     this.user_surname=user_surname;
     return this;
   }

   age(user_age) {
     this.user_age=user_age;
     return this;
   }

   get(callback) {

     var list ={};

     list.uname=this.user_name;
     list.usurname=this.user_surname;
     list.uage=this.user_age;

     callback(list);
   }

}

module.exports = bar;

app.js

var express=require("express"),
app=express();


app.get("/liste/:ext",function(req,res){

  var ext=req.params.ext;

  res.setHeader('Content-Type', 'application/json');

  if(ext==1)
  {
    var bar=new require('./test');
    bar.name("alex").surname("broox").age(32).get(function(result){
      res.json({data:result})
    })

  }

  if(ext==2)
  {
    var bar=new require('./test');
    bar.name("alex2").get(function(result){
      res.json({data:result})
    })
  }

})

app.listen(4000,function(log){
  console.log("listening")
})

as error : TypeError: bar.name is not a function

Upvotes: 0

Joe Clay
Joe Clay

Reputation: 35807

test.js isn't valid syntax - you shouldn't have your entire file wrapped in braces like that. module.exports is just a variable which you set; if you want to export bar, set it to bar:

class bar {
   ...
}

module.exports = bar;

Additionally, you need to assign the result of your require call in app.js.

var bar = require("./test");

(On a slightly pedantic note - it's more idiomatic to captialize your class names!)

Upvotes: 4

Related Questions