2016-02-14 32 views
1

應該很簡單,但找不到問題。gulp-nodemon + browser-sync:在服務器端代碼發生更改後,應用程序不會重新加載

模板對公共文件所做的更改全部通過瀏覽器同步進行更新。但是,對app.js,./bin/www./route/**/*.js的更改會導致瀏覽器同步刷新,但顯然不會觸發nodemon重新啓動應用程序:我需要停止並手動重新啓動它。

我運行使用DEBUG=appName:* node ./bin/www & gulp

這裏我的應用程序是我Gulpfile.js

////////////////////////////////////// 
 
// Simple task to update our views // 
 
////////////////////////////////////// 
 

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

 
// the real stuff 
 
gulp.task('default', ['browser-sync'], function() { 
 
\t gulp.watch('./views/**/*.jade', browserSync.reload); 
 
\t gulp.watch(['./routes/**/*.js', './bin/www', './app.js'], ['bs-reload-delay']); 
 
}); 
 

 
// our browser-sync config + nodemon chain 
 
gulp.task('browser-sync', ['nodemon'], function() { 
 
\t browserSync.init(null, { 
 
\t \t proxy: "http://localhost:3000", 
 
     files: ["public/**/*.*"], 
 
     browser: "chromium-browser", 
 
     port: 4000, 
 
\t }); 
 
}); 
 

 
// our delayed reload for our server side js 
 
gulp.task('bs-reload-delay', function() { 
 
    setTimeout(function() { 
 
    browserSync.reload({ stream: false }); 
 
    }, 800); 
 
}); 
 

 
// our gulp-nodemon task 
 
gulp.task('nodemon', function (cb) { 
 
\t var started = false; 
 
\t return nodemon({ 
 
\t \t script: './app.js', 
 
\t \t ext: 'js', 
 
\t \t task: ['bs-reload-delay'] 
 
\t }).on('start', function() { 
 
\t \t // to avoid nodemon being started multiple times 
 
\t \t if (!started) { 
 
\t \t \t cb(); 
 
\t \t \t started = true; 
 
\t \t } 
 
\t }).on('crash', function() { 
 
\t \t console.log('nodemon.crash'); 
 
\t }).on('restart', function() { 
 
\t \t console.log('nodemon.restart'); 
 
\t }); 
 
});

這裏是我的目錄

. 
 
├── app.js 
 
├── bin 
 
│   └── www 
 
├── config.js 
 
├── Gulpfile.js 
 
├── npm-debug.log 
 
├── package.json 
 
├── public 
 
│   ├── css 
 
│   │   └── style.css 
 
│   ├── favicon.ico 
 
│   ├── img 
 
│   └── js 
 
│    └── front-client.js 
 
├── readme.md 
 
├── routes 
 
│   ├── client.js 
 
│   ├── fire.js 
 
│   └── game.js 
 
└── views 
 
    ├── client.jade 
 
    ├── error.jade 
 
    └── _layout.jade

回答

3

好吧,算了一下。也許這可能對別人有用。問題是由吞噬文件和我如何啓動我的應用造成的:DEBUG=appName:* node ./bin/www & gulp

gulp-nodemon 已經在gulp中啓動我的應用程序,因此不需要在gulp之前調用節點。另外,我現在使用env屬性來傳遞我的DEBUG & NODE_ENV變量。現在,要在開發模式下啓動我的應用程序,我只需運行gulp。這裏是我的Gulpfile.js

////////////////////////////////////// 
 
// Simple task to update our views // 
 
////////////////////////////////////// 
 

 
var gulp = require('gulp'); 
 
var nodemon = require('gulp-nodemon'); 
 
var bs = require('browser-sync').create(); 
 

 
// our browser-sync config + nodemon chain 
 
gulp.task('browser-sync', ['nodemon'], function() { 
 
\t bs.init(null, { 
 
\t \t proxy: "http://localhost:3000", 
 
\t \t browser: "chromium-browser", 
 
\t \t port: 4000, 
 
\t }); 
 
}); 
 

 
// the real stuff 
 
gulp.task('default', ['browser-sync'], function() { 
 
\t gulp.watch('./views/**/*.jade', bs.reload); 
 
\t gulp.watch('./public/**/*.js', bs.reload); 
 
\t gulp.watch('./public/**/*.css', bs.reload); 
 
\t gulp.watch(['./routes/**/*.js', './app.js', './bin/www'], ['bs-delay']); 
 
}); 
 

 
// give nodemon time to restart 
 
gulp.task('bs-delay', function() { 
 
    setTimeout(function() { 
 
    bs.reload({ stream: false }); 
 
    }, 1000); 
 
}); 
 

 
// our gulp-nodemon task 
 
gulp.task('nodemon', function (cb) { 
 
\t var started = false; 
 
\t return nodemon({ 
 
\t \t script: './bin/www', 
 
\t \t ext: 'js', 
 
\t \t ignore: ['public/**/*.js'], 
 
\t \t env: { 
 
\t \t \t 'NODE_ENV': 'development', 
 
\t \t \t 'DEBUG': 'appname:*' 
 
\t } 
 
\t }).on('start', function() { 
 
\t \t //avoid nodemon being started multiple times 
 
\t \t if (!started) { 
 
\t \t \t cb(); 
 
\t \t \t started = true; 
 
\t \t } 
 
\t }) 
 
\t .on('crash', function() { 
 
\t \t // console.log('nodemon.crash'); 
 
\t }) 
 
\t .on('restart', function() { 
 
\t \t // console.log('nodemon.restart'); 
 
\t }) 
 
\t .once('quit', function() { 
 
\t \t // handle ctrl+c without a big weep 
 
\t \t process.exit(); 
 
\t }); 
 
});

相關問題