Accessing parameters of request object at cloud end : getting req.params empty

261 views
Skip to first unread message

gauri....@gmail.com

unread,
May 22, 2013, 11:12:40 AM5/22/13
to node...@googlegroups.com
Hi,

I created Node.ACS project(cloud end) to implement custom cloud services & Titanium mobile project (client end) from which I used to call the cloud services which I have implemented at cloud side. Following are the steps which I have followed to achieve this client & cloud interaction. After that I have mentioned the problem.


1. Created new Node.ACS Project ‘MyCloud'
2. Added a New Service 'greet' & its code is as below:
file name: services.js

function greet(req, res) {
console.log('In greet method:' );
var no = req.params.number1;
var no2 = req.params.number2;
var jsonObj = {
"result" : no + no2,
"status": 200
};
res.json(jsonObj);
};

My config.js file looks like this:
{
    "routes": [ {
        "path": "/",
        "callback": "application#index"
    }, {
        "path": "/greet",
        "method": "post",
        "callback": "services#greet"
    } ],
    "filters": [ {
        "path": "/",
        "callback": ""
    } ],
    "websockets": [ {
        "event": "",
        "callback": ""
    } ]
}

3. Published App & got a full url:https://<appid>.cloudapp.appcelerator.com
4. Created a New Mobile App ‘MyClient’
5. Imported the Node Project into a index.js in Titanium
6. I see the code that got added to index.js which now looks like this:

// @autogenerated:start:ti.cloud.MyCloud
var MyCloud = require("ti.cloud.MyCloud");
// @autogenerated:end:ti.cloud.MyCloud

function doClick(e) {  
    var params = {
    "number1" : 5,
    "number2" : 10
    }
    var greet_resp = MyCloud.services_greet(params, function  (r,e) {
      
      alert('callback' + JSON.stringify(r));
    });
    
}

$.index.open();


My ti.cloud.MyCloud.js looks like:
function InvokeService(path, method, data, cb) {
    if ("function" == typeof data) {
        cb = data;
        data = null;
    }
    var xhr = Ti.Network.createHTTPClient();
    "function" == typeof cb && (xhr.onload = function(e) {
        var r = this.responseText;
        if (-1 != xhr.getResponseHeader("content-type").indexOf("json")) try {
            r = JSON.parse(r);
        } catch (E) {}
        cb(r, e);
    });
    "/" == exports.URL.match("/$") && 0 == path.indexOf("/") ? xhr.open(method, exports.URL + path.substring(1)) : xhr.open(method, exports.URL + path);
    xhr.send(data);
}

var url = Ti.App.Properties.getString("acs-service-baseurl-MyCloud");

alert("URL" + url);

exports.URL = url && url.replace(/^\s+|\s+$/g, "") ? url.replace(/^\s+|\s+$/g, "") : "http://localhost:8080";

exports.application_index = function(data, cb) {
    InvokeService("/", "GET", data, cb);
};

exports.services_greet = function(data, cb) {
    alert("data : " + JSON.stringify(data) + "type" + typeof data);
    InvokeService("/greet", "POST", data, cb);
};

Problem:
On cloud end, in greet() function in services.js file I tried to access the number1 & number2 parameters but i got the req.params empty. What is the structure of req object? How can i access the values sent from client side?
When I executed JSON.stringify(req), it returned  "TypeError: Converting circular structure to JSON" error.
When I executed JSON.stringify(req.params), it gave me the "[]" on console .
Please provide some solution for accessing request parameters.

Peter Lee

unread,
May 22, 2013, 12:06:39 PM5/22/13
to node...@googlegroups.com
If you are sending parameter as post data, it should be on req.body.

gauri...@mindstix.com

unread,
May 23, 2013, 2:05:10 AM5/23/13
to node...@googlegroups.com
Thanks Peter, Problem solved.. Thanks for quick reply & useful link !!!
Reply all
Reply to author
Forward
0 new messages