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.
-
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. – Spoike Aug 20 '13 at 04:39
-
@Spoike Actually I'm new to these technologies and dont know anything...about node. – user2655757 Aug 20 '13 at 04: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 – user2655757 Aug 20 '13 at 04:49
2 Answers
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
-
Thanks for ur reply... at last...im getting error: performbatch not found. – user2655757 Aug 20 '13 at 05:47
-
@user2655757 Oops, I thought angular-seed was using express. See basti1302's answer for how to do things in server-side. – Spoike Aug 20 '13 at 06:36
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.
-
-
AFAIK angular-seed just scaffolds the Angular app, it has nothing to do with any backend technology like Node or Express. – basti1302 Aug 20 '13 at 06:46
-
-
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. – basti1302 Aug 20 '13 at 06:57
-
You can easily test that without the Angular part. Just open your browser and go to http://localhost:8888/performbatch. This will trigger the batch file. (Of course you need to start node web-server.js before) – basti1302 Aug 20 '13 at 06:58
-
but when i run your web-server.js i'm getting message 'Could not process your request, sorry.' – user2655757 Aug 20 '13 at 06:58
-
@user2655757 you need to make webserver.js both serve the static html/css/js files as well as handle the path /performbatch in the request – Spoike Aug 20 '13 at 07:06
-
thanks a lot...I got.. but that is not my target.. when i click a button on angular-seed app , the batch file should be executed(called) from node.js that batch file simply appends date to log file.. @Spoike helped some extent – user2655757 Aug 20 '13 at 07:07
-
@user2655757 you can try adding the `if (path=='performbatch') {...}` part of this solution to where angular-seed is handling it's requests [in scripts/web-server.js here](https://github.com/angular/angular-seed/blob/master/scripts/web-server.js#L90). – Spoike Aug 20 '13 at 07:20
-
It might be much easier (and less confusing) to use a separate node process for the backend and one for serving the AngularJS frontend. That angular-seed comes with an integrated node server to serve its html files is one thing, but using that server to also be the backend might not be a good idea, because (as you have seen) it can be messy because you need to route certain calls through angular routing to the backend. Better make a completely separate node.js process for the batch execution thing and one for serving Angular conten. – basti1302 Aug 20 '13 at 11:05