grunt-express-server with contrib-watch

南笙酒味 提交于 2019-12-19 04:04:37

问题


I am trying to use both grunt-express-server and grunt-contrib-watch however, as soon as my express server starts, it no longer seems to do any watching or reloading. I have the server setup to spawn in the background. My project is here: https://github.com/RyanHirsch/firem

Here is my Gruntfile.js

module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);
  // Project configuration.
  grunt.initConfig({
    watch: {
      options: {
        livereload: true,
      },
      express: {
        files:  [ 'index.html', 'server.js' ],
        tasks:  [ 'express:dev' ],
        options: {
          spawn: false
        }
      }
    },
    express: {
      options: {
        // Override defaults here
      },
      dev: {
        options: {
          script: 'server.js'
        }
      }
    }
  });

  grunt.registerTask('default', ['express:dev','watch']);
};

回答1:


I was able to clone your project and able to get everything running with the following tweak in server.js:

app.listen(3000);

into:

app.listen(3000, function() {
  console.log('Server listening on port 3000');
});

According to the grunt-express-server's "Usage" docs, your server should console.log some output so that the grunt task can tell that the server has started successfully.

(This is because starting the server is asynchronous, which can cause a race-condition with LiveReload)

Otherwise, there is a delay option for purists who don't want any output from their application :)



来源:https://stackoverflow.com/questions/21502339/grunt-express-server-with-contrib-watch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!