从应用角度的NodeJS调用函数 [英] Call function in nodejs from angular application

查看:119
本文介绍了从应用角度的NodeJS调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角应用(角种子应用程序),应调用一个的NodeJS功能(网络server.js)。
在该的NodeJS功能只是调用批处理文件。

I'm having an angular app(angular-seed app) which should call a function in nodejs(web-server.js). The function in nodejs is just calls a batch file.

推荐答案

该任择议定书没有提到前preSS所以我没有使用任何额外的框架,提供了服务器端(Node.js的一部分)的替代(这可能需要通过NPM安装它)。该解决方案只使用节点的核心:

The OP didn't mention express so I'll provide an alternative for the server side (Node.js part) without using any additional frameworks (which would require installing it via npm). This solution uses just node core:

网络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上,我会在第一次使用批处理文件这样来测试吧:

Assuming you are on Windows, I would at first use a batch file like this to test it:

whatever.bat:

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

有关客户端,有什么要添加到 Spoike的解决方案

For the client side, there is nothing to add to Spoike's solution.

这篇关于从应用角度的NodeJS调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆