Ben Aston Ben Aston - 1 month ago 19
Node.js Question

Preventing the process from exiting when running a gulp task (so a server remains up)

I have a gulp task that starts a server:

gulp.task('my-task', ['start-server'], function() {});


The gulp task
start-server
registers a listener:

process.on('exit', function () {
server.stop();
});


But, I want the server to remain open. A bit like a
gulp-watch
task stays "open".

Currently, AFAICT, the Node.js process running the gulp task, exits upon completion of the gulp task and takes down the server with it.

How can I avoid the process exiting and taking the server down with it?

Answer

That's what gulp-nodemon was meant to do: https://www.npmjs.com/package/gulp-nodemon

Example implementation:

gulp.task('develop', function () {
  nodemon({ script: 'server.js'
          , ext: 'html js'
          , ignore: ['ignored.js']
          , tasks: ['lint'] })
    .on('restart', function () {
      console.log('restarted!')
    })
})
Comments