我的代碼,工程類..如何要求的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")
})
but..following代碼不起作用。與需要從其他文件類..
測試。 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文件。與要求類
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")
})
但它爲什麼不工作...請幫助我..上面的代碼,但工作,但此代碼不起作用..
感謝您的解決方案 –