【问题标题】:Gulp/LiveReload – Frequently Getting Error... Error: listen EADDRINUSEGulp/LiveReload – 经常出错... 错误:听 EADDRINUSE
【发布时间】:2014-09-14 23:33:24
【问题描述】:

当我停止服务器并通过在终端中按 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);
});

});

【问题讨论】:

  • 您是否在错误处理程序或 node.on('close') 处尝试过 lrServer.close()?
  • 我马上试试……
  • 目前工作正常。谢谢你:)
  • 太棒了!我会继续添加它作为答案

标签: node.js express gulp


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-11-21
    • 2014-12-11
    • 2017-07-28
    • 1970-01-01
    • 2016-09-16
    • 2015-04-15
    • 2015-05-31
    • 1970-01-01
    • 2016-06-28
    相关资源
    最近更新 更多