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-20903 http://rhumaric.com/2014/01/livereload-magic-gulp-style/
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);
});
There are lots of plugins like gulp-livereload
, connect-livereload
, gulp-connect
, gulp-watch
so how can I get this wired up?
Live reload should work with any nodejs script. Here is a good gist.
Here's my solution:
You also need to include connect-livereload in your express server:
Include it before bodyParser. I have read that this is not needed if you have the chrome live reload extension.
I had the same issue and was not able to find anything related. My solution ends up with the following piece of code in
Gulpfile
:You'll need to install
tiny-lr
,async
,gulp-util
andgulp-load-plugins
packages for this sample to work. I guess that I'll create a separate Gulp plugin for it =)Take a look at gulp-nodemon which will restart your server when code changes.
Example:
I've added code that
Detects changes in server files and reloads the server via nodemon
Waits for a couple seconds after process reload in order to give the server time to run its initialization code.
Triggers a change in a livereload server
note 1 : Your build should also include a livereload server and attach livereload scripts to html files before calling the 'serve' task
note 2: This is an asynchronous task that never ends, do not use it as a dependency of other tasks
You can see the setup I used at http://github.com/arvsr1988/gulp-expressjs-setup