標籤:div net res pts targe app.js bin route detail
搬運自【http://blog.csdn.net/zhu_free/article/details/51476525】
gulp-express實現即時重新整理
本來使用gulp-connect可以建立本機伺服器,配合Livereload就可以實現即時重新整理,但express項目內建了伺服器,就不太好辦了,之前用了gulp-express,實現了一部分,感覺很方便,但是只有jade模板變化會即時重新整理,樣式表和js檔案則不會,之前的部分代如下:
gulp.task(‘server‘, [‘styles‘, ‘scripts‘], function() { express.run([‘./bin/www‘]); //啟動 gulp.watch(‘src/scss/**/*.scss‘, [‘styles‘]); //監視樣式表 gulp.watch(‘src/js/**/*.js‘, [‘scripts‘]); //監視js檔案 gulp.watch(‘views/**/*.jade‘, express.notify); //監視模板檔案 gulp.watch([‘app.js‘, ‘routes/**/*.js‘], express.run);});gulp.task(‘default‘, [‘server‘]);
找了一些資料提到一些gulp-livereload, connect-livereload各種,還是沒有想出來解決辦法,最後忽然想到是不是gulp-express已經內建了,然後注意到express.notify這個函數貌似就是重新整理的意思,於是嘗試在styles和scripts方法裡面結尾加上express.notify(),結果並沒有什麼卵用,然後試著在watch方法的callbacks列表後面加上notify:
gulp.watch(‘src/scss/**/*.scss‘, [‘styles‘, express.notify]); //監視樣式表gulp.watch(‘src/js/**/*.js‘, [‘scripts‘, express.notify]); //監視js檔案
還是沒有作用→_→
然後試著在模板檔案的路徑前面加上樣式表和js檔案的路徑:
gulp.watch([‘views/**/*.jade‘, ‘src/js/**/*.js‘, ‘src/scss/**/*.scss‘], express.notify);
最後的解決辦法是。。。代碼加上了這兩句
gulp.watch(‘public/**/*.css‘, express.notify);gulp.watch(‘public/**/*.js‘, express.notify);
總結完整代碼
gulp.task(‘server‘, [‘styles‘, ‘scripts‘], function() { express.run([‘./bin/www‘]); //啟動 gulp.watch(‘src/scss/**/*.scss‘, [‘styles‘]); //監視樣式表 gulp.watch(‘src/js/**/*.js‘, [‘scripts‘]); //監視js檔案 gulp.watch(‘views/**/*.jade‘, express.notify); //監視模板檔案 gulp.watch([‘app.js‘, ‘routes/**/*.js‘], express.run); gulp.watch(‘public/**/*.css‘, express.notify); gulp.watch(‘public/**/*.js‘, express.notify);});gulp.task(‘default‘, [‘server‘]);
nodejs裡的express自動重新整理gulp-express使用【轉載】