Gulp / LiveReload - 经常出错......错误:听EADDRINUSE

时间:2014-07-23 16:44:48

标签: node.js express gulp

当我停止服务器并通过在终端中按CTRL + C重新启动它时,我收到以下错误:

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)
    ...

目前,我通过在终端中键入killall -9 node来解决此问题,但如何停止接收此错误,是否与我的Gulpfile.js有关?

// Module Dependencies
var gulp = require('gulp'),


gconcat = require('gulp-concat'),
gzip = require('gulp-gzip'),
jshint = require('gulp-jshint'),
csslint = require('gulp-csslint'),
ngmin = require('gulp-ngmin'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
lr = require('tiny-lr'),
spawn = require('child_process').spawn,
lrServer = lr(),
node;

// Location Arrays – (Note: The Order in Array is important as it reflects the order of loading...)
 var serverjsLocations = ['./app/controllers/*.js', './app/controllers/api/*.js', './app/controllers/api/v1/*.js', './app/lib/*.js', './app/models/*.js', './config/env/*.js', './config/middlewares/*.js', './config/*.js', '*.js'],
dashboardjsLocations = ['./public/js/dashboard/**/*.js'],
homejsLocations = ['./public/js/home/**/*.js'],
alljsLocations = serverjsLocations.concat(dashboardjsLocations, homejsLocations),
cssLocations = ['./public/css/*.css'];


// JS hint task
gulp.task('jsLint', function() {
gulp.src(alljsLocations)
    .pipe(jshint())
    .pipe(jshint.reporter('jshint-stylish'));
});

// CSSLint Task
gulp.task('cssLint', function() {
gulp.src(cssLocations)
    .pipe(csslint())
    .pipe(csslint.reporter());
});

// Build Task
gulp.task('build', function() {
gulp.src(dashboardjsLocations)
    .pipe(gconcat('dashboard.js'))
    .pipe(ngmin())
    .pipe(rename('dashboard.min.js'))
    .pipe(gulp.dest('./public/dist'))
    .pipe(uglify())
    .pipe(gulp.dest('public/dist'))
    .pipe(gzip())
    .pipe(gulp.dest('public/dist'));
// .pipe(refresh(lrServer));
gulp.src(homejsLocations)
    .pipe(gconcat('home.js'))
    .pipe(ngmin())
    .pipe(rename('home.min.js'))
    .pipe(gulp.dest('./public/dist'))
    .pipe(uglify())
    .pipe(gulp.dest('public/dist'))
    .pipe(gzip())
    .pipe(gulp.dest('public/dist'));
// .pipe(refresh(lrServer));
});

// Server Task
gulp.task('server', function() {
    if (node) node.kill();
    node = spawn('node', ['server.js'], {
        stdio: 'inherit'
    });
    node.on('close', function(code) {
        if (code === 8) {
            console.log('Error detected, waiting for changes...');
        }
    });
});


// Watch Statements
gulp.task('default', ['jsLint', 'build', 'server'], function() {

gulp.watch(alljsLocations, ['build', 'server'], function() {});

lrServer.listen(35731, function(err) {
    if (err) return console.log(err);
});

});

1 个答案:

答案 0 :(得分:1)

在错误处理程序或node.on('close')

中使用lrServer.close()