ameni
ameni

Reputation: 393

Starting and Stopping node.js script automatically

i want to run this node. js script without using command prompt :

    var spawn = require('child_process').spawn;
// Run node with the GenerateBlob.js file as an argument
var child = spawn('node', ['GenerateBlob.js']);
// Listen for any response from the child:
child.stdout.on('data', function (data) {
    var result = data.toString();
});
// Listen for any errors:
child.stderr.on('data', function (data) {
    console.log('There was an error: ' + data);
});

How can i run this script from another javascript program (i don't mean child_process), any other program js that allow me to run this node js script and get the output without using the command prompt.

Upvotes: 4

Views: 413

Answers (1)

CodePhobia
CodePhobia

Reputation: 1315

Okay, so given the information. This is what I would do. Assuming this is in you dev environment.

Using Mocha with Gulp to keep a background thread running.

So this is what you need to do.

  1. Include mocha dependency in your package.json

  2. Describe your activity to do be done in a test.js file. i.e. Run the other JS file in here. for e.g I used the following to test my db connection js file.

    var assert = require('assert');
    var connect = require('./connect');
    var dbInterface = require('./interface');
    
    describe('dbInterface', function() {
    //Do Whatever you want to do with the interface.js File }
    
  3. You need to use gulp to have a background process continually.

a. Include gulp and gulp-mocha in your dependecies.

In your gulpfile.js have it run whatever you need continuously, in here I am making it watch test.js and interface.js i.e. on every change in them re run the tests.

var gulp = require('gulp');
var mocha = require('gulp-mocha');

gulp.task('watch', function() {
  gulp.watch(['./test.js', './interface.js'], ['test']);
});

b. npm install

c. run npm run watch on a separate command window and continue with you day job.

Hope this helps. You can include whatever logic you want in the test.js file. and call ANY required function of ANY JS file which you refer inside of it.

EDIT: Usage with the code required.

You can make your current script this way, say your script.js

exports.script = function(req,callback){
//Your Script above.
    var spawn = require('child_process').spawn;
// Run node with the GenerateBlob.js file as an argument
var child = spawn('node', ['GenerateBlob.js']);
// Listen for any response from the child:
child.stdout.on('data', function (data) {
    var result = data.toString();
});
// Listen for any errors:
child.stderr.on('data', function (data) {
    console.log('There was an error: ' + data);
});
});

Now, Inside your test.js

var assert = require('assert');
var childInterface= require('./script');

describe('testingscript', function() {

  it('can run the script successfully', function(done) {
    childInterface.script(null, function(error) {
      assert.ifError(error);
      console.log('wahtever message');
    });
  });

Take care of the callbacks and params. After this if you run,

.\node_modules\.bin\mocha test.js

You can see what the results being output and technically you are not running node srcipt.js on your file.

I am sure there are other ways. Do let me know if you find a more easier one.

Upvotes: 2

Related Questions