4

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?

Community
  • 1
  • 1
Kode
  • 3,073
  • 18
  • 74
  • 140
  • 1
    Despite both using javascript, Node is a *server* technology while angular runs in the browser. You cannot include a script intended for node in your browser because the browser does not and should not know it exists. You'll have to make a [route](http://expressjs.com/guide/routing.html), serve it, and call it in angular. – Jorg Sep 11 '15 at 04:06

1 Answers1

9

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

maddygoround
  • 2,145
  • 2
  • 20
  • 32
  • This is starting to clear some things up. What is the purpose of exports then? – Kode Sep 11 '15 at 04:44
  • Could I pass variables from Angular back to node for execution (ex. Blob container name as in my example?) – Kode Sep 11 '15 at 04:46
  • 1
    exports do the job of module importing in NodeJS,its like creating nodejs function in different file and importing it in some other file – maddygoround Sep 11 '15 at 04:47
  • Perfect, so exports are more meant for using functions in another node file, not so much client side function calling? – Kode Sep 11 '15 at 11:50
  • 1
    @maddygoround is mentioning localhost in http.get enough even if the server is deployed somewhere else? or do we need to replace it with actual server address? – siva Oct 04 '16 at 06:50
  • you need to replace it with your server URL.Server URL is the one where your node code is deployed. – maddygoround Oct 04 '16 at 10:55
  • so its actually make a request through the internet or go directly from angular to node? – dang Nov 01 '16 at 07:52