问题描述
我正在尝试将我的 gulpfile.js
设置为在快速服务器上使用 livereload,但运气不佳.我看到了一些例子,但它们似乎与静态文件服务器有关.
I am trying to setup my gulpfile.js
to use livereload on an express server without much luck. I see examples out there but they seem to be related to a static file server.
http://code.tutsplus.com/tutorials/gulp-as-a-development-web-server--cms-20903http://rhumaric.com/2014/01/livereload-magic-gulp-style/
所以我有一个 app.js
文件,它使用玉文件等执行标准快递服务器.我想要做的是让它与 gulp.js 引导中的 livereload 一起工作.
So I have an app.js
file which does the standard express server with jade files, etc. What I want to do is get it to work with livereload from a gulp.js boot.
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
有很多插件,如 gulp-livereload
、connect-livereload
、gulp-connect
、gulp-watch代码> 那么我怎样才能把它连接起来呢?
There are lots of plugins like gulp-livereload
, connect-livereload
, gulp-connect
, gulp-watch
so how can I get this wired up?
推荐答案
我已经添加了代码
检测服务器文件的变化并通过nodemon
在进程重新加载后等待几秒钟,以便让服务器有时间运行其初始化代码.
Waits for a couple seconds after process reload in order to give the server time to run its initialization code.
触发 livereload 服务器的变化
Triggers a change in a livereload server
注意 1:在调用服务"任务之前,您的构建还应包括一个 livereload 服务器并将 livereload 脚本附加到 html 文件
note 1 : Your build should also include a livereload server and attach livereload scripts to html files before calling the 'serve' task
注意 2:这是一个永不结束的异步任务,不要将其用作其他任务的依赖项
note 2: This is an asynchronous task that never ends, do not use it as a dependency of other tasks
gulp.task('serve', function (cb) {
nodemon({
script : <server start file>,
watch : <server files>
//...add nodeArgs: ['--debug=5858'] to debug
//..or nodeArgs: ['--debug-brk=5858'] to debug at server start
}).on('start', function () {
setTimeout(function () {
livereload.changed();
}, 2000); // wait for the server to finish loading before restarting the browsers
});
});
这篇关于gulp.js livereload 与快递服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!