ericclemmons / grunt-express-server

Grunt task for running an Express Server that works great with LiveReload + Watch/Regarde

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

server not stopping before restart when editing Gruntfile.js

pensierinmusica opened this issue · comments

Hi,

I have Grunt "watch" looking at some files and restarting Express if they get changed.

It all works seamlessly. For some weird reason though if the file edited is Gruntfile.js, grunt-express-server doesn't stop Express before relaunching when the express:dev task is triggered by grunt-watch and obviously it generates an EADDRINUSE error.

This is my grunt-watch configuration:

    watch: {
      options: {
        livereload: true
      },
      stylus: {
        files: ['<%= yeoman.app %>/styles/sections/*.styl'],
        tasks: ['stylus'],
        options: {livereload: false}
      },
      autoprefixer: {
        files: ['<%= yeoman.app %>/styles/*.css'],
        tasks: ['autoprefixer']
      },
      express: {
        files: [
          'app.js',
          'config/*.js',
          'controllers/*.js',
          'models/*.js',
          'Gruntfile.js'
        ],
        tasks: ['express:dev'],
        options: {spawn: false} // Without this option specified express won't be reloaded
      }

And this is my grunt-express-server configuration:

    express: {
      options: {
        port: process.env.PORT || 3000
      },
      dev: {
        options: {
          script: 'app.js'
        }
      },
      prod: {
        options: {
          script: 'app.js',
          node_env: 'production'
        }
      }
    }

The weird thing is that the problem ony happens with Gruntfile.js. If I modify, let's say, app.js or one of the controllers, the task correctly stops Express before reloading it.

Any thoughts?? Thanks!

I'm not really sure yet, but I have to ask, why restart the server if the Gruntfile changes?

I guess you're right... I could just live-reload the browser :)

I'm having this issue when I'm editing my package.json and wanting to reload the server to include new packages like middleware and the likes.

Wow, that's really odd! I was able to reproduce it, & trying to figure out why Gruntfile.js changes would cause this!

Apparently Gruntfile.js is auto-watched, which doesn't properly emit the proper signals to kill the server. As a result, the next iteration of watch seems to run the task in a new namespace, meaning the server never shuts down.

Trying an alternative now...

Aha! Stupid Grunt being clever. I'm having to introduce process._servers to keep the server in memory for stopping, since Grunt explicitly clears the require cache.