【问题标题】:Gulp Watch - gulp-nodemon - App CrashedGulp Watch - gulp-nodemon - 应用程序崩溃
【发布时间】:2015-12-25 12:30:48
【问题描述】:

我正在尝试设置 Gulp 来观看一件事:服务器的源代码。源更新后,服务器节点脚本将重新启动,客户端浏览器将刷新。

我相信我需要 gulp-nodemon 用于服务器,而 browser-sync 用于客户端。

服务器的脚本被执行:node src\babel.js
该脚本在以这种方式运行时有效,但通过我的 Gulp 配置失败。

是不是我做错了什么?

这是我的任务脚本:

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



gulp.task('default', ['watchServer', 'watchClient']);

gulp.task('watchServer', function() {
    gulp.watch('src/**', function () {
        nodemon({ // called upon update
            script: 'src/babel.js',   // I have tried both / & \\
        })
    });

    nodemon({ // called on start
        script: 'src/babel.js',   // I have tried both / & \\
    })
});

gulp.task('watchClient', function() {
    browserSync({
        open: 'external',
        host: '████████',
        port: 80,
        ui: false,
        server: {
            // We're serving the src folder as well
            // for sass sourcemap linking
            baseDir: ['src']
        },
        files: [
         'src/**'
        ]
    });
});

日志:

> gulp

[02:28:04] Using gulpfile B:\Test Server\gulpfile.js
[02:28:04] Starting 'watchServer'...
[02:28:04] Finished 'watchServer' after 19 ms
[02:28:04] Starting 'watchClient'...
[02:28:04] Finished 'watchClient' after 27 ms
[02:28:04] Starting 'default'...
[02:28:04] Finished 'default' after 9.66 μs
[02:28:04] [nodemon] 1.7.1
[02:28:04] [nodemon] to restart at any time, enter `rs`
[02:28:04] [nodemon] watching: *.*
[02:28:04] [nodemon] starting `node src\babel.js`
[BS] Access URLs:
 -----------------------------------
    Local: http://localhost:80
 External: http://████████:80
 -----------------------------------
[BS] Serving files from: src
[BS] Watching files...
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::80
    at Object.exports._errnoException (util.js:837:11)
    at exports._exceptionWithHostPort (util.js:860:20)
    at Server._listen2 (net.js:1231:14)
    at listen (net.js:1267:10)
    at Server.listen (net.js:1363:5)
    at B:/Test Server/src/app/app.jsx:17:7
    at Object.<anonymous> (B:/Test Server/src/app/app.jsx:41:2)
    at Module._compile (module.js:434:26)
    at normalLoader (B:\Test Server\node_modules\babel-core\lib\api\register\node.js:199:5)
    at Object.require.extensions.(anonymous function) [as .jsx] (B:\Test Server\node_modules\babel-core\lib\api\register\node.js:216:7)
[02:28:05] [nodemon] app crashed - waiting for file changes before starting...

【问题讨论】:

    标签: node.js gulp gulp-watch


    【解决方案1】:

    你不需要用 gulp 观察服务器文件,因为 nodemon 会在它改变时自动重启,在你的配置中尝试这样的操作

    gulp.task('watchServer', function() {
        // remove the gulp.watch entry
    
        nodemon({ // called on start
            script: 'src/babel.js',   // I have tried both / & \\
            ext: 'js',
            watch: ['src/babel.js']
        })
    });
    

    似乎还有其他东西在端口 80(这是 apache 的默认端口)上运行,因此将 browsersync 运行的端口更改为类似 4000 可能会有所帮助

    如果您的节点服务器运行在端口 3000 上,您将需要使用 browsersync 代理它。

    gulp.task('watchClient', function() {
        browserSync({
            open: 'external',
            host: '████████',
            proxy: '[YOURHOST]:3000'
            port: 4000,
            ui: false,
            // this entry will most likely need to be removed
            // if you are using something like express as a static server
            server: {
                // We're serving the src folder as well
                // for sass sourcemap linking
                baseDir: ['src']
            },
            files: [
             'src/**'
            ]
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2017-06-21
      • 2015-05-18
      • 2014-06-23
      • 2018-03-05
      • 2014-07-21
      • 2015-10-10
      • 2018-02-17
      • 1970-01-01
      • 2019-06-22
      相关资源
      最近更新 更多