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?

1
  • 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, serve it, and call it in angular. Commented Sep 11, 2015 at 4:06

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

This is starting to clear some things up. What is the purpose of exports then?
Could I pass variables from Angular back to node for execution (ex. Blob container name as in my example?)
exports do the job of module importing in NodeJS,its like creating nodejs function in different file and importing it in some other file
Perfect, so exports are more meant for using functions in another node file, not so much client side function calling?
@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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.