Connect LiveReload Middleware

0.6.1 · abandoned · verified Wed Apr 22

connect-livereload is a Connect/Express middleware designed to inject the LiveReload script directly into HTML responses. This eliminates the need for a browser extension, simplifying the development workflow by automatically reloading the browser upon file changes detected by companion tools like `grunt-contrib-watch`. The current stable version is 0.6.1, which was published approximately 7 years ago (December 2018), indicating that the package is largely unmaintained. Its primary differentiation is the server-side injection, which contrasts with client-side browser extensions. It does not, however, serve the `livereload.js` script itself, requiring another module (e.g., a LiveReload server like `node-livereload`) to provide it. Configuration options allow for specifying the LiveReload server port, ignoring certain file types (e.g., `.js`, `.css`), or customizing the HTML detection and injection rules.

Common errors

Warnings

Install

Imports

Quickstart

This example sets up an Express server with `connect-livereload` and a separate `livereload` server. It demonstrates how to integrate the middleware to inject the LiveReload script into static and dynamic HTML responses, enabling automatic browser reloads upon file changes. It highlights correct middleware placement and the necessity of a separate LiveReload server.

const express = require('express');
const path = require('path');
const livereload = require('livereload');
const connectLiveReload = require('connect-livereload');

// Initialize a LiveReload server
const lrserver = livereload.createServer();
lrserver.watch(path.join(__dirname, 'public'));

const app = express();
const port = 3000;

// Use connect-livereload middleware *before* static routes to inject into HTML
// For dynamic content, place it after static routes but before actual dynamic route handlers.
app.use(connectLiveReload({
  port: 35729,
  // Ignore common non-HTML files by default
  ignore: [/.js(\?.*)?$/, /.css(\?.*)?$/, /.svg(\?.*)?$/, /.ico(\?.*)?$/, /.woff(\?.*)?$/, /.png(\?.*)?$/, /.jpg(\?.*)?$/, /.jpeg(\?.*)?$/, /.gif(\?.*)?$/, /.pdf(\?.*)?$/]
}));

// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));

// Example route for dynamic content (which will also be reloaded)
app.get('/dynamic', (req, res) => {
  res.send(`<!DOCTYPE html>
<html lang="en">
<head><title>Dynamic Page</title></head>
<body>
  <h1>Welcome to the dynamic page!</h1>
  <p>Current time: ${new Date().toLocaleTimeString()}</p>
</body>
</html>`);
});

app.listen(port, () => {
  console.log(`Express app listening at http://localhost:${port}`);
  console.log(`LiveReload server running on port 35729, watching ${path.join(__dirname, 'public')}`);
});

// Create a 'public' directory and an 'index.html' inside for testing:
// echo '<!-- index.html -->\n<!DOCTYPE html>\n<html lang="en">\n<head><title>Home</title></head>\n<body>\n  <h1>Hello World!</h1>\n  <p>Edit this file to see live reload.</p>\n</body>\n</html>' > public/index.html

view raw JSON →