Using connect-livereload function in Express node app

Following this great article on how to use npm as a build tool, I would like to implement it when building a nodejs express web app. My node app is built on port 8080, this is a simplified version of my file server.js

:

var env = process.env.NODE_ENV

var path = require('path');
var express = require('express');
var logger = require('morgan');

var routes = require('./routes');

var app = express();

app.set('port', process.env.PORT || 8080);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.engine('jade', require('jade').__express)

var Oneday = 86400000;
app.use(express.static(__dirname + '/www', {
    maxAge: env == 'development' ? 0 : Oneday
}));

app.use(logger('dev'));

app.use(express.static(path.join(__dirname, '/public'), {
    maxAge: env == 'development' ? 0 : Oneday
}))

if (env == 'development') {
    // var liveReloadPort = 9091;
    app.use(require('connect-livereload')({
        port: 8080
        // src: "js/"
            // port: liveReloadPort
    }));
}

routes.blog(app);
routes.frontend(app, passport);

app.use(function(err, req, res, next) {
    console.log(err.stack);
    res.status(500).send({
        message: err.message
    })
});

app.listen(app.get('port'));
console.log('Server starting on port: ' + app.get('port'));

      

The file I'm viewing before rebooting is in www/js

. I use it npm

as a build tool and before starting server.js

with npm I run a separate process that does watchify source/js/app.js -v -o wwww/js/bundle.js

I have verified that the tracking system is working correctly by updating when I save my files. But after the file has been changed, there is no stress on the liver. The error I get in the console is: Uncaught SyntaxError: Unexpected token <

and I see that connect-livereload has inserted this script into the html:

<script>
//<![CDATA[
document.write('<script src="//' + (location.hostname || 'localhost') + ':8080/livereload.js?snipver=1"><\/script>')
//]]>
</script>
<script src="//localhost:8080/livereload.js?snipver=1"> </script>

      

I have also tried using live-reload as mentioned in the original article but with no success and I'm not sure if this is the right plugin to use as it live-reload

starts the server when I already start it with express. Any ideas?

+3


source to share


1 answer


connect-livereload only inserts the script into the html (so you don't need a browser plugin).

you still need a separate download server, try node-livereload , grunt-contrib-connect or grunt-contrib-watch ... ever since you want to use npm as your build tool, the former should be recommended.

then you will have to change the furnace load port to the current server to reboot the server (the default port is 35729):

  app.use(require('connect-livereload')({
    port: 35729
  }));

      

The server you tried: live-reload should work as well.



Can you try and start with:

live-reload [<path>...] --port=35729 --delay=someDelay

      

and change the connect-livereload parameter to:

  app.use(require('connect-livereload')({
    src: "localhost:35729"
  }));

      

+3


source







All Articles