1

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.

3
  • What have you tried so far? You could create a route that when called will perform the function that calls the batch file in the server. Commented Aug 20, 2013 at 4:39
  • @Spoike Actually I'm new to these technologies and dont know anything...about node. Commented Aug 20, 2013 at 4:47
  • my target is to call a function in web-server.js upon clicking a button/link from angular app.. the function is calls batch file using ('child_process').spawn help me out Commented Aug 20, 2013 at 4:49

2 Answers 2

11

If I understood this correctly you want a click on the client-side (angular app) to call a batch file on the server side. You can do this in several ways depending on your requirements, but basically you want the client-side to send a http-request to the server (either with ajax call or form submit) and process this on the server that will call the batch file.

Client-side

On the client-side you need to have a button that uses the angular ng-click directive:

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

In your angular controller you'll need to use the $http service to make a HTTP GET request to your server on some particular url. What that url is depends how you've set up your express app. Something like this:

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

}

You can validate that this HTTP GET request is made by using e.g. your browser's developer tools such as Google Chrome's network tab or a http packet sniffer such as fiddler.

Server-side

EDIT: I incorrectly assumed that angular-seed was using expressjs, which it doesn't. See basti1302's answer on how to set it up server-side "vanilla style" node.js. If you're using express you can continue below.

On the server side you need to set up the url in your express app that will perform the batch file call. Since we let the client-side above make a simple HTTP GET request to /performbatch we'll set it up that way:

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

Calling the batch file is done in some ways but you can read the stackoverflow answer here for a solution:

Hope this helps

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

2 Comments

Thanks for ur reply... at last...im getting error: performbatch not found.
@user2655757 Oops, I thought angular-seed was using express. See basti1302's answer for how to do things in server-side.
5

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:

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)

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

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

10 Comments

Oops. I thought angular-seed was using express. :-)
AFAIK angular-seed just scaffolds the Angular app, it has nothing to do with any backend technology like Node or Express.
@basti1302 how my request will be passed to onRequest function
The last line (http.createServer(onRequest).liste(8888) opens port 8888 to listen for incoming http requests. Once a request is received the function you pass into the createServer method is called. In this example, this is exactly the onRequest function. So the answer is: Node.js' http module calls your onRequest function, whenever a http request comes in.
You can easily test that without the Angular part. Just open your browser and go to localhost:8888/performbatch. This will trigger the batch file. (Of course you need to start node web-server.js before)
|

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.