res.setHeader('Content-Type', 'application/json');
Hope this helps
I’ve been trying to implement the Financial Stock Watch List Node.ACS project , with some success, though I’m blocked on how to call the service from a Titanium App, this is what I’ve done:1- Created new Node.ACS Project ‘NodeFinanDemo’
2- Added a New Service ‘refreshPrice’ & pasted the code blow, and updated the ACS Oath & Secret key, and created a Custom Object to a user ‘stocks’ with a property called ‘symbol’ and value ‘APPL’.
var ACS = require('acs').ACS; var logger = require('acs').logger; var EventEmitter = require('events').EventEmitter; var https = require('http'); var sessionID = null; var stocks; ACS.init('*****', '*****'); // used my ACS OAuth key and secret function refreshPrice(req, res) { var uid = req.query.uid; var Event = new EventEmitter; var results = [], evt_count = 0; // SETUP EVENT LISTENER Event.on("HTTP_COMPLETE", function (e) { console.log("EVENT: HTTP_COMPLETE"); evt_count++; console.log(evt_count); if (evt_count >= stocks.length) { console.log("close response, results = " + JSON.stringify(results)); res.write(JSON.stringify(results)); res.end(); } }); ACS.Objects.query({ page: 1, per_page: 100, classname: "stocks", where: { user_id: uid, } }, function (data) { console.log(JSON.stringify(data)); stocks = data.stocks; var l = stocks.length; if (l == 0) { Event.emit("HTTP_COMPLETE"); }; for (var i = 0; i < l; i++) { marketOnDemandQuote(stocks[i].symbol); } }) function marketOnDemandQuote(symbol) { var myreq = https.get('http://dev.markitondemand.com/Api/Quote/json?symbol=' + symbol, function (r) { r.setEncoding('utf8'); r.on('data', function (chunk) { console.log(chunk); var o = {}, d = JSON.parse(chunk); o.symbol = d.Data.Symbol; o.name = d.Data.Name; o.lastPrice = d.Data.LastPrice; o.change = d.Data.Change; o.changePercent = d.Data.ChangePercent; o.timestamp = d.Data.Timestamp; results.push(o); Event.emit("HTTP_COMPLETE"); }); }); } }; exports.refreshPrice = refreshPrice; // Source code attribution: Asim Siddiqui and Bert Grantges
3- Published App & got a full url:https://<appid>.cloudapp.appcelerator.com
4- Created a New Mobile App ‘NodeACSAppConnection’
5- Imported the Node Project into a FirstView.js in Titanium
6- I see the code that got added to FirstView.js which now looks like this:
// @autogenerated:start:ti.cloud.NodeFinanDemo
var NodeFinanDemo = require("ti.cloud.NodeFinanDemo");
// @autogenerated:end:ti.cloud.NodeFinanDemo
var Cloud = require('ti.cloud');
function FirstView() {
var self = Ti.UI.createView();
Cloud.Users.login({
login: 'default',
password: 'imemine'
}, function (e) {
if (e.success) {
var user = e.users[0];
NodeFinanDemo.refreshPrice({ }, function (_e) {
Ti.API.info(_e);
});
} else {
alert('Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
return self;
}
module.exports = FirstView;
7- No idea how to call the refreshPrice from the Ti App?
Note: I breakpointed the line `NodeFinanDemo.refreshPrice` and peaked into NodeFinanDemo:
URL: “http://localhost:53945″ // even though I can access that locally, shouldn’t this be https://<appid>.cloudapp…com ?
uri: “app://ti.cloud.NodeFinanDemo.js”
id: “ti.cloud.NodeFinanDemo”Also if I run the code above, I get a runtime error:
message = “‘undefined’ is not a function (evaluating ‘NodeFinanDemo.refreshPrice({ }, function (_e) {\n Ti.API.info(_e);\n })’)”;
password: '******'
}, function (e) {
if (e.success) {
var user = e.users[0];
NodeFinanDemo.refreshPrice({ }, function (_e) {
Ti.API.info(_e);
});
} else {
alert('Error:\n' +
((e.error && e.message) || JSON.stringify(e)));
}
});
return self;
}
module.exports = FirstView;
7- No idea how to call the refreshPrice from the Ti App?
Note: I breakpointed the line `NodeFinanDemo.refreshPrice` and peaked into NodeFinanDemo:
URL: “http://localhost:53945″ // even though I can access that locally, shouldn’t this be https://<appid>.cloudapp…com ?
uri: “app://ti.cloud.NodeFinanDemo.js”
id: “ti.cloud.NodeFinanDemo”
Also if I run the code above, I get a runtime error:
message = “‘undefined’ is not a function (evaluating ‘NodeFinanDemo.refreshPrice({ }, function (_e) {\n Ti.API.info(_e);\n })’)”;
function simpleReturn(req, res) {
res.setHeader('Content-Type', 'application/json'); // Added for Mobile Apps
res.send('Hello, world!');
};
exports.simpleReturn = simpleReturn;
function simplecall(){
NodeFinanDemo.services_simpleReturn({}, callback);
function callback(_e) {
Ti.API.info(_e);
};
}