This is my Gruntfile:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
serve: {
files: ['server.js', 'src/**/*.coffee'],
tasks: ['coffee', 'develop'],
options: {
nospawn: true
}
},
css: {
files: ['lib/less/main.less'],
tasks: ['less'],
options: {
nospawn: true
}
},
test: {
...
}
},
jasmine_node: {
...
},
develop: {
server: {
file: 'server.js'
}
},
coffee: {
compile: {
expand: true,
bare: true,
cwd: 'src/',
src: ['**/*.coffee'],
dest: 'lib/',
ext: '.js'
}
},
copy: {
...
},
jasmine: {
...
},
less: {
..
},
concurrent: {
options: {
logConcurrentOutput: true
},
serve: {
tasks: ["watch:css", "watch:serve"]
},
}
});
grunt.loadNpmTasks('grunt-contrib-coffee');
...
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-concurrent');
grunt.registerTask('serve', ['coffee', 'develop', 'concurrent:serve']);
grunt.registerTask('test', ['coffee', 'jasmine_node'/*, 'watch:test'*/]);
grunt.registerTask('build', ['coffee', 'less']);
grunt.registerTask('templates', ['copy']);
};
Problem: the first time it starts my server well, after editing coffee files my server throws error EADDRINUSE, but the url is still accessible (so the first server wasn't shutdown). Full project: http://github.com/OpenCubes/OpenCubes
[grunt-develop] >
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1042:14)
at listen (net.js:1064:10)
at net.js:1146:9
at dns.js:72:18
at process._tickCallback (node.js:419:13)
>> application exited with code 8
The behavior that you specify is as expected. When you start your watch task, it fires up your server on the specified port. However, when you save, the watch task tries to fire up the server again on the same port, but a server instance is already running on that port. Thus, you get the EADDRINUSE
error because the port is already in use.
When you kill your grunt task, it kills the process, which includes the server that you're running.
To solve your problem (though the question is somewhat unclear), you need to kill the server before you start a new server on that same port. The easiest way to do that is probably to include a module like grunt-nodemon or one of the many modules specifically for express
.
Also, if it's for your tests that you need to run the server, you don't need to have your server listen to a port if you're using supertest to test your APIs.