【发布时间】:2015-12-07 12:36:20
【问题描述】:
让我在这个前缀上加上我是 Node/Express 的新手。
我有一个 AngularJS 应用程序,它利用 Node.JS 来管理 Azure Blob 要求,例如创建 Blob 容器,如下所示:
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');
从 Node.js 中的 server.js 执行时创建容器的功能按预期工作并创建一个 blob 容器。但是,我需要在我的 AngularJS 应用程序中单击时创建一个 blob 容器。我设想使用导出来访问和执行在 Server.js 中创建的函数,但看到了一些混合信息,特别是当 Express.js 在图片中时,通过 AngularJS 客户端调用 Node.js 函数,因为它出现在 Angular 应用程序中必须进行 http 调用(请参阅这篇文章中的最后一个答案:Call function in nodejs from angular application)。
我的问题如下:
1) 由于我的应用程序当前使用 Node、Express 和 Angular,我是否需要在我的 Angular 控制器中使用 http 来运行 Node 函数/是否所有用 Node/Server.js 编写的函数都需要 $http 才能在调用时执行通过AngularJS客户端即使他们不调用服务但可能是执行诸如数学之类的函数?基于 Express 的调用示例:
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) 或者正在使用导出,例如在这篇文章中列出的更常见的做法,其中函数定义为导出,然后通过要求导入:What is the purpose of Node.js module.exports and how do you use it?。如果是这样,我会做类似以下的事情吗?:
节点 Server.JS 文件:
var myFunc1 = function() { ... };
exports.myFunc1 = myFunc1;
在 AngularJS 控制器中(不包括作为依赖项):
var m = require('pathto/server.js');
m.myFunc1();
3) 最后,我是否完全偏离了基础,并且有一种从我缺少的 Angular 控制器调用 node.js 函数的常见做法?
【问题讨论】:
-
尽管两者都使用 javascript,但 Node 是一种 服务器 技术,而 Angular 在浏览器中运行。您不能在浏览器中包含用于节点的脚本,因为浏览器不知道也不应该知道它存在。你必须创建一个route,服务它,并以角度调用它。