Grunt开发EADDRINUSE

这是我的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']); }; 

问题:第一次启动我的服务器,编辑咖啡文件后,我的服务器抛出错误EADDRINUSE,但url仍然可以访问(所以第一台服务器没有关机)。 完整的项目: 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 

您指定的行为与预期的一样。 当你开始你的监视任务时,它启动指定端口上的服务器。 但是,保存时,监视任务会尝试在同一端口再次启动服务器,但服务器实例已在该端口上运行。 因此,您将收到EADDRINUSE错误,因为该端口已被使用。

当你杀死你的咕噜任务时,它会杀死包含你正在运行的服务器的进程。

为了解决你的问题(尽pipe这个问题有些不清楚),你需要在相同的端口上启动一个新的服务器之前杀死服务器。 最简单的方法可能是包含一个像grunt-nodemon这样的模块,或者是专门用于express模块之一。

另外,如果你的testing需要运行服务器,如果你使用supertest来testing你的API,你不需要让服务器监听端口。