Greetings,
I am working on a web application using the MEAN Stack. At this point I have succeeded in logging in and setting a token in session storage and also listing my devices on a separate page. Now the third page is giving me some trouble.
For clarification purposes this application is using Angular and Node to perform the critical parts…
So in the view that I need to publish a function I am not using a form. In fact, I am only using two buttons as such:
<button class="btn btn-primary" ng-click="klaxonOn()">Klaxon On</button>
<button class="btn btn-danger" ng-click="klaxonOff()">Klaxon Off</button>
This calls a function inside my controller:
(function(){
angular
.module('klaxon')
.controller('klaxonCtrl', klaxonCtrl);
klaxonCtrl.$inject = ['$scope', 'dataservice', '$window'];
function klaxonCtrl($scope, dataservice, $window){
$scope.klaxonOn = function(){
dataservice.klaxonOn(window.sessionStorage.getItem('token'))
.then(function success(res){
console.log("klaxonOn");
console.log(res);
},function error(err){
console.log(err);
});
}
$scope.klaxonOff = function(){
dataservice.klaxonOff(window.sessionStorage.getItem('token'))
.then(function success(res){
console.log("klaxonOff");
console.log(res);
},function error(err){
console.log(err);
});
}
}
})();
As you may have spotted this gets sent to a dataservice as follows:
(function(){
angular
.module('klaxon')
.service('dataservice', dataservice);
dataservice.$inject = ['$http'];
function dataservice($http){
this.login = function(username, password){
var data = {
username: username,
password: password
};
return $http.post('/api/login', data);
};
this.listDevices = function(token){
return $http.get('/api/getdevices/' + token);
};
this.klaxonOn = function(token){
return $http.post('/api/klaxonOn/' + token);
};
this.klaxonOff = function(token){
return $http.post('/api/klaxonOff/'+ token);
};
}
})();
The last two functions are the only two of concern at this point. So from here I pass my token from this being on the client side to the API of my application in which I have literally pasted the code generated by postman which was successful at publishing an event to the console logs in particle. For the sake of brevity I will only post one of those as they are both the same with the exception of the data being “on” or “off” :
var qs = require("querystring");
var http = require("https");
module.exports.klaxonOn = function(req, res){
var token = req.params.token;
var options = {
"method": "POST",
"hostname": "api.particle.io",
"port": null,
"path": "/v1/devices/events?access_token=" + token,
"headers": {
"content-type": "application/x-www-form-urlencoded",
"cache-control": "no-cache",
}
};
var req = http.request(options, function (resp) {
var chunks = [];
resp.on("data", function (chunk) {
chunks.push(chunk);
});
resp.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
res.status(200).json({'success': true});
});
});
req.write(qs.stringify({ name: 'klaxon', data: 'on' }));
req.end();
};
Please bear in mind this worked in postman but for some reason, not quite publishing the events from my web application. Any thoughts? Help?
Cheers,