Let me prefix this with that I am newer to Node/Express.
I have an AngularJS App that is leveraging Node.JS for managing Azure Blob requirements such as creating Blob containers as follows:
function test(containerName) {
blobSvc.createContainerIfNotExists(containerName, function (error, result, response) {
if (!error) {
// Container exists and allows
// anonymous read access to blob
// content and metadata within this container
}
});
};
test('blob4');
The function for creating a container when executed from server.js in Node works as expected and creates a blob container. However, I need to create a blob container on click in my AngularJS app. I envisioned using exports to access and execute functions created in Server.js but have seen some mixed information, especially when Express.js is in the picture, for calling a Node.js function via AngularJS client side since it appears that in an Angular App an http call would have to be made (please see the last answer in this post: Call function in nodejs from angular application).
My questions are as follows:
1) Since my app currently uses Node, Express, and Angular, would I need to use the http in my Angular controller to run Node functions/do all functions written in Node/Server.js require $http to execute if called via AngularJS client side even if they don't call a service but might be a function performing something such as math? Example of an Express based call:
function MyCtrl($scope, $http) {
// $http is injected by angular's IOC implementation
// other functions and controller stuff is here...
// this is called when button is clicked
$scope.batchfile = function() {
$http.get('/performbatch').success(function() {
// url was called successfully, do something
// maybe indicate in the UI that the batch file is
// executed...
});
}
}
2) Or is using exports, such as listed in this post more common practice, where the function is defined as an export and then imported via a requires: What is the purpose of Node.js module.exports and how do you use it?. If so, would I do something like the following?:
Node Server.JS File:
var myFunc1 = function() { ... };
exports.myFunc1 = myFunc1;
Within an AngularJS Controller (not including as a dependency):
var m = require('pathto/server.js');
m.myFunc1();
3) Lastly, am I completely off base and there is a common practice for calling node.js functions from an Angular Controller that I am missing?
First of all nodejs and angularjs all though both are javascript both are two different implementation.
NodeJS works on server, on other hand angularjs works on browser.
initially when i was newbie to node i was also having the same problem . i was thinking we could directly call the node function from angularjs ,after all everything is javascript right ! but i was wrong..
Now here how you should do this
First create a route in nodejs (its nothing,just create a simple restAPI)
app = express();
app.get('/dowork',function(res,req){
console.log(req.params.msg);
/... code to do your work .../
});
now in angularjs call do work
$http.get('http://localhost:8080/dowork',{"msg":"hi"}).success(function(data){
console.log(data);
});
Im not sure will it req.params.msg
but you can log req
and can find the object.
In case of post request your parameters will be in req.body
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With