Gulp BrowserSync 和 npm 启动

Gulp BrowserSync and npm start

我想使用 BrowserSync 启动我的 NodeJS 应用程序,但它似乎不起作用。

错误信息:错误:监听 EADDRINUSE

编辑#1:

我找到了解决问题的方法。

gulp.task('sync', ['start', 'watch'], function(cb) {
    browserSync.init({
        files: ['**/*'],
        proxy: 'http://localhost:3000',
        port: 4000
    });
});

gulp.task('start', function() {
    var called = false;
    return nodemon({
        script: './bin/www',
        watch: ['Application.js']
    }).on('start', function onStart() {
        if (!called) {
            cb();
        }
        called = true;
    }).on('restart', function onRestart() {
        setTimeout(function reload() {
            browserSync.reload({
                stream: false
            });
        }, 500);
    });
});

EADDRINUSE 表示 listen() 尝试绑定服务器的端口号已被使用。因此,在您的情况下,必须已经有 运行 端口 80 上的服务器。如果您在此端口上有另一个网络服务器 运行,您将收到 EADDRINUSE 错误。

var gulp = require('gulp');
var browserSync = require('browser-sync');
var nodemon = require('gulp-nodemon');

gulp.task('default', ['browser-sync'], function () {
});

gulp.task('browser-sync', ['nodemon'], function() {
    browserSync.init(null, {
        proxy: "http://localhost:5000",
        files: ["**/*.*"],
        browser: "google chrome",
        port: 7000,
    });
});

gulp.task('nodemon', function (cb) {
    nodemon({
      script: './bin/www/app.js'
    })
    .on('start', function () {
      cb();
    })
    .on('error', function(err) {
     // Make sure failure causes gulp to exit
     throw err;
   });
});

希望对您有所帮助:)