tmuecksch tmuecksch - 8 months ago 62
Node.js Question

Gulp: Run multiple node scripts in parallel

I've got two server scripts (both are relying on; running on different ports).

I'd like to start both in parallel via gulp. But in addition I'd like to have a possibility to stop one of them. And maybe even access the console output of each script.

Is there any existing solution for this? Or would you even recommend using anything else than gulp?


I found a solution in which I additionally start a mongoDB server:

var child_process = require('child_process');
var nodemon = require('gulp-nodemon');

var processes = {server1: null, server2: null, mongo: null};

gulp.task('start:server', function (cb) {
    // The magic happens here ...
    processes.server1 = nodemon({
        script: "server1.js",
        ext: "js"

    // ... and here
    processes.server2 = nodemon({
        script: "server2.js",
        ext: "js"

    cb(); // For parallel execution accept a callback.
          // For further info see "Async task support" section here:

gulp.task('start:mongo', function (cb) {
    processes.mongo = child_process.exec('mongod', function (err, stdout, stderr) {});


process.on('exit', function () {
    // In case the gulp process is closed (e.g. by pressing [CTRL + C]) stop both processes

gulp.task('run', ['start:mongo', 'start:server']);
gulp.task('default', ['run']);