【问题标题】:Call function in nodejs from angular application从角度应用程序调用nodejs中的函数
【发布时间】:2013-08-22 00:45:07
【问题描述】:

我有一个角度应用程序(角度种子应用程序),它应该调用 nodejs(web-server.js)中的一个函数。 nodejs中的函数只是调用一个批处理文件。

【问题讨论】:

  • 到目前为止您尝试过什么?您可以创建一个路由,在调用该路由时将执行调用服务器中的批处理文件的功能。
  • @Spoike 实际上我是这些技术的新手,对节点一无所知。
  • 我的目标是在单击 Angular 应用程序中的按钮/链接时调用 web-server.js 中的函数。该函数是使用 ('child_process') 调用批处理文件。spawn 帮助我

标签: javascript html node.js angularjs request


【解决方案1】:

如果我理解正确,您希望单击客户端(角度应用程序)以在服务器端调用批处理文件。您可以根据您的要求以多种方式执行此操作,但基本上您希望客户端向服务器发送 http 请求(使用 ajax 调用或表单提交)并在将调用批处理文件的服务器上处理它.

客户端

在客户端,您需要有一个使用角度 ng-click 指令的按钮:

<button ng-click="batchfile()">Click me!</button>

在您的角度控制器中,您需要使用$http service 在某个特定网址上向您的服务器发出 HTTP GET 请求。该网址是什么取决于您如何设置您的快速应用程序。像这样的:

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...
        });
    }

}

您可以使用例如验证此 HTTP GET 请求是否发出。您浏览器的开发工具,例如 Google Chrome's network tab 或 http 数据包嗅探器,例如 fiddler

服务器端

编辑:我错误地认为 angular-seed 使用的是 expressjs,但事实并非如此。见basti1302's answer on how to set it up server-side "vanilla style" node.js。如果您使用的是 express,您可以在下面继续。

在服务器端,您需要set up the url in your express app 来执行批处理文件调用。由于我们让上面的客户端向/performbatch 发出一个简单的 HTTP GET 请求,我们将这样设置它:

app.get('/performbatch', function(req, res){
    // is called when /performbatch is requested from any client

    // ... call the function that executes the batch file from your node app
});

调用批处理文件以某些方式完成,但您可以在此处阅读 stackoverflow 答案以获得解决方案:

希望对你有帮助

【讨论】:

  • 感谢您的回复...终于...我收到错误:找不到执行批处理。
  • @user2655757 糟糕,我认为 angular-seed 使用的是 express。有关如何在服务器端执行操作,请参阅 basti1302 的答案。
【解决方案2】:

OP 没有提到 express,所以我将为服务器端(Node.js 部分)提供一个替代方案,而不使用任何额外的框架(这需要通过 npm 安装它)。此解决方案仅使用节点核心:

web-server.js:

'use strict';

var http = require('http')
var spawn = require('child_process').spawn
var url = require('url')

function onRequest(request, response) {
  console.log('received request')
  var path = url.parse(request.url).pathname
  console.log('requested path: ' + path)
  if (path === '/performbatch') {
    // call your already existing function here or start the batch file like this:
    response.statusCode = 200
    response.write('Starting batch file...\n')
    spawn('whatever.bat')
    response.write('Batch file started.')
  } else {
    response.statusCode = 400
    response.write('Could not process your request, sorry.')
  }
  response.end()
}

http.createServer(onRequest).listen(8888)

假设你在 Windows 上,我首先会使用这样的批处理文件来测试它:

whatever.bat:

REM Append a timestamp to out.txt
time /t >> out.txt

对于客户端,Spoike's solution 无需添加任何内容。

【讨论】:

  • 糟糕。我以为角种子正在使用快递。 :-)
  • AFAIK angular-seed 只是 Angular 应用程序的支架,它与 Node 或 Express 等任何后端技术无关。
  • @basti1302 我的请求将如何传递给 onRequest 函数
  • 最后一行 (http.createServer(onRequest).liste(8888) 打开端口 8888 以侦听传入的 http 请求。一旦收到请求,就会调用传递给 createServer 方法的函数。在这个例子,这正是 onRequest 函数。所以答案是:Node.js 的 http 模块调用你的 onRequest 函数,只要有一个 http 请求进来。
  • 您可以在没有 Angular 部分的情况下轻松测试它。只需打开浏览器并转到localhost:8888/performbatch。这将触发批处理文件。 (当然需要先启动node web-server.js)
猜你喜欢
  • 2019-12-17
  • 1970-01-01
  • 2016-06-29
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
相关资源
最近更新 更多