This script manages a tiny LiveReload server
implementation.
It exposes an HTTP server and express middleware, with a very basic REST
API to notify the server of a particular change.
It doesn't have any watch ability, this must be done at the build process or
application level.
Instead, it exposes a very simple API to notify the server that some
changes have been made, then broadcasted to every connected livereload client.
# notify of a single changecurl http://localhost:35729/changed?files=style.css# notify using a longer pathcurl http://localhost:35729/changed?files=js/app.js# notify of multiple changes, comma or space delimitedcurl http://localhost:35729/changed?files=index.html,style.css,docs/docco.css
Or you can bulk the information into a POST request, with the body as a JSON array of files.
curl -X POST http://localhost:35729/changed -d '{ "files": ["style.css", "app.js"] }'# from a JSON filenode -pe 'JSON.stringify({ files: ["some.css", "files.css"] })' > files.jsoncurl -X POST -d @files.json http://localhost:35729
The best way to integrate the runner into your workflow is to add it as a reload
step within your build tool.
var tinylr = require('tiny-lr');// standard LiveReload portvar port = 35729;// tinylr(opts) => new tinylr.Server(opts);tinylr().listen(port, function() { console.log('... Listening on %s ...', port);})
You can define your own route and listen for a specific request:
var server = tinylr();server.on('GET /myplace', function(req, res) { res.write('Mine'); res.end();})
And stop the server manually:
server.close();
This will close any websocket connection established and emit a close event.
Middleware
To use as a connect / express middleware, tiny-lr needs query /
bodyParser middlewares prior in the stack (to handle POST requests)
Any handled requests ends at the tinylr level, not found and errors are
nexted to the rest of the stack.
var port = process.env.LR_PORT || process.env.PORT || 35729;var path = require('path');var express = require('express');var tinylr = require('tiny-lr');var body = require('body-parser');var app = express();// This binds both express app and tinylr on the same portapp .use(body()) .use(tinylr.middleware({ app: app })) .use
The port you listen on is important, and tinylr should always listen on
the LiveReload standard one: 35729. Otherwise, you won't be able to rely
on the browser extensions, though you can still use the manual snippet
approach.
You can also start two different servers, one on your app port, the
other listening on the LiveReload port.
See the make-livereload repo.
This repository defines a bin wrapper you can use and install with:
npm install make-livereload -g
It bundles the same bin wrapper previously used in the tiny-lr repo.
Usage: tiny-lr [options]Options: -h, --help output usage information -V, --version output the version number port -p pid Path to the generated PID file (default: ./tiny-lr.pid)