如何在node.js中要求一个类

Spa*_*roy 3 node.js

我的代码有效..

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")
})
Run Code Online (Sandbox Code Playgroud)

但..以下代码不起作用..要求来自其他文件的类..

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);
   }

}

};
Run Code Online (Sandbox Code Playgroud)

app.js文件..用require那个类

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")
})
Run Code Online (Sandbox Code Playgroud)

但为什么它不起作用...请帮助我..上面的代码有效,但这段代码不起作用..

Dom*_*ana 14

在节点中导出类时,首先需要定义类,然后使用module.exports后面的类导出要导出的类.

// 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
Run Code Online (Sandbox Code Playgroud)

从那里你可以只是require文件,并抓住这样的类.

var Bar = require('./test');
var bar = new Bar();
Run Code Online (Sandbox Code Playgroud)


Joe*_*lay 4

test.js不是有效的语法 - 你不应该将整个文件用这样的大括号括起来。module.exports只是您设置的一个变量;如果要导出bar,请将其设置为bar

class bar {
   ...
}

module.exports = bar;
Run Code Online (Sandbox Code Playgroud)

require此外,您需要将调用的结果分配给app.js.

var bar = require("./test");
Run Code Online (Sandbox Code Playgroud)

(稍微迂腐一点——将类名大写更符合习惯用法!)