简体   繁体   English

Gulp Watch - gulp-nodemon - App崩溃

[英]Gulp Watch - gulp-nodemon - App Crashed

I'm trying to set up Gulp to watch 1 thing: server's source. 我正在尝试设置Gulp来观察一件事:服务器的来源。 Upon source update, the server node script will start over & client browser will refresh. 源更新后,服务器节点脚本将重新开始,客户端浏览器将刷新。

I believe I need gulp-nodemon for the server, while browser-sync for the client. 我相信我需要gulp-nodemon用于服务器,而浏览器同步用于客户端。

The server's script is executed by: node src\\babel.js 服务器的脚本由: node src \\ babel.js执行
The script works when ran that way, but fails through my config for Gulp. 该脚本在以这种方式运行时可以正常工作,但是通过我的Gulp配置失败了。

Is there something I am doing wrong? 有什么我做错了吗?

This is my task script: 这是我的任务脚本:

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/**'
        ]
    });
});

Log: 日志:

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

You don't need to watch the server file with gulp since nodemon will automatically restart when it changes, try something like this in your config 您不需要使用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']
    })
});

There also seems to be something else running on port 80 (this is the default port for apache) so it might help to change the port browsersync is running on to something like 4000 在端口80上似乎还有其他东西在运行(这是apache的默认端口)所以它可能有助于将浏览器同步端口更改为类似于4000的端口

If your node server is running on let's say port 3000 you will need to proxy it with browsersync for example. 如果您的节点服务器正在运行,例如端口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/**'
        ]
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM