var console = process.console; var unirest = require('unirest'); 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/AddChannels/getName").send(data).end(function(response){ console.log(response.body); 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} }) };