问题
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