item.js
4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var console = process.console;
var unirest = require('unirest');
var moment = require('moment');
module.exports = function(Item) {
Item.disableRemoteMethod("create", true);// post
Item.disableRemoteMethod("find", false);// get
Item.disableRemoteMethod("findById", true);// get{id}
Item.disableRemoteMethod("findOne", false);// get findOne
Item.disableRemoteMethod("deleteById", false);// delete {id}
Item.Greet = function(ctx,cb){
var res = {
'resultCode':20000,
'resultDescription':'Success',
'data':{
'xx':'xx'
}
};
cb(null,res);
};
Item.remoteMethod('Greet',{
http:{path:'/Greet',verb:'get'},
accepts: { arg: 'ctx', type: 'object', http: { source: 'body' } } ,
returns: {arg: 'data', type: 'object'}
});
Item.MyFuncion =function(ctx,cb){
// ctx ค่าที่รับมาจาก accepts ถ้าไม่มีการส่งค่ามา ค่าที่ส่งเข้าไปให้กับ function ตัวนั้นจะเป็น callback
console.tag("Get").log("Get data from remoteMethod"+ctx);
};
Item.remoteMethod('MyFuncion',{
http:{path:'/:itemName',verb:'get'},
accepts: { arg: 'ctx', type: 'string', http: function(ctx) {
// รับข้อมุลมาใส่ไว้ใน ctx
// 1. รับข้อมูล object ที่ส่งมาโดยใช้ express
var req = ctx.req;
/* 2. Get ข้อมูลแบบ params
* params คือค่าที่ส่งมากับ url ex http://localhost/Item/params1/params2
* รับค่ามาตั้งแต่ http:{path:'/: ตัวแปรที่รับค่าเก็บไว้ /: ตัวแปรที่รับค่าเก็บไว้
*/
return req.params.itemName;
} } ,
returns: {arg: 'data', type: 'string', root: true}
});
Item.Api = function(ctx,cb){
var data = {
"name":ctx
};
unirest.post("http://localhost:3000/api/customer/getName").send(data).end(function(response){
cb(null,response.body)
});
};
Item.remoteMethod('Api',{
http:{path:'/Api/:name',verb:'get'},
accepts: { arg: 'ctx', type: 'string'},
returns: {arg: 'data', type: 'string', root: true}
});
Item.Insert = function(ctx,cb){
console.tag("Get").log(ctx);
var response;
Item.create(ctx,function(err,result){
if(err){
response = {
"resultCode" : 50000,
"resultDescription":"Fail",
"data":{
"error":err
}
};
cb(null,response);
console.tag("Response").log(response);
}else{
response = {
"resultCode" : 20000,
"resultDescription":"Success",
"data":result
};
cb(null,response);
console.tag("Response").log(response);
}
});
};
Item.remoteMethod('Insert',{
http:{path:'/',verb:'post'},
accepts: { arg: 'ctx', type: 'object', default: {
"item_name": "string",
"item_desc": "string",
"quantity": 0,
"price": 0
},http: { source: 'body' }},
returns: {arg: 'data', type: 'object', root: true}
});
Item.Moment = function(cb){
var this_time = {
"default": moment().format(),
"DD-MM-YYY":moment().format("DD-MM-YYYY"),
"HH:MM:SS":moment().format("HH:mm:ss")
};
cb(null,this_time);
};
Item.remoteMethod('Moment',{
http:{path:'/Moment',verb:'get'},
returns: {arg: 'data', type: 'object', root: true}
})
};