Serve-live: static directory server with change notification

Overview

serve-live: static directory server with change notification

This is a web server that serves up a static directory, but also provides server-sent events when files in that directory changes, so that web pages can automatically refresh.

The served address is hard-coded as 0.0.0.0:3000. This should be visible both from localhost and from the network. The served directory defaults to the current directory, but you can pass the path as an argument on the command line.

To hook up the automatic refresh, I put the following code in auto-reload.js:

{ events.close(); location.reload() }) events.onerror = (err) => { console.error("server-sent event source 'event' failed: ", err); };">
var events = new EventSource("events");
events.addEventListener("files-changed", (event) => {
    events.close();
    location.reload()
})

events.onerror = (err) => {
    console.error("server-sent event source 'event' failed: ", err);
};

Then I load it as a module from my HTML file (say, index.html):

">
  

When I visit localhost:3000, that serves me index.html, which loads the module, which subscribes to update events at http://localhost:3000/events, and arranges to reload the tab whenever I touch anything. Done!

Type serve-live --help for more details.

But npm start does this already.

Indeed, and much more!

I wanted to do some JavaScript experimentation, and I wanted my browser tab to automatically refresh when I changed files. If you're writing a React app, you can use npx create-react-app to set something up that does this very nicely, and it puts you right in the sweet spot with React, webpack, JSX, and all that jazz set up and ready to go.

But I find it distracting to have so much preprocessing going on between me and the browser. For example, when you say:

import './App.css';

in your JavaScript file, I get that this somehow applies the CSS rules in App.css to the components you're defining, in a way that helps webpack understand dependencies, etc.

But what does all that actually mean? Don't the CSS files imported by all your modules just get concatenated? I went to look this up, but the webpack docs referred me to a blog post about "the 'Block, Element, Modifier' methodology" for organizing CSS rules... and I just timed out. I don't want to criticize things I don't understand (Chesterton's Fence and all that), but ultimately for my purposes the JS stack there is a distraction of a scale proportional to its depth.

And that depth is profound indeed:

$ npx create-react-app smoove
...
Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...

added 1367 packages in 26s
...
Happy hacking!
$ cd smoove
$ du -sh .
300M	.
$ 

When I work on implementing the web platform itself, I want to know exactly what's happening at the level of the content APIs the browser exposes. I'm not trying to build anything useful in JS, I'm trying to understand an API's behavior. Every layer that introduces a difference between what I wrote and what happens in the browser is a distraction for me.

There are tons of other crates that do this already.

Yep. But there's a saying in science: "Two months in the lab can save you half a day at the library!"

You might also like...
client-server notification center for dbus desktop notifications

tsuchita A client-server notification center for dbus desktop notifications. Specifically org.freedesktop.Notifications dbus messages. Motivation I ju

Notification demon + web server using async Rust
Notification demon + web server using async Rust

Async Rust example Road to the asynchronous Rust Table of Contents About the Project Screenshots Tech Stack Features Getting Started Prerequisites Clo

Simple personal server to serve audiofiles files from folders.

Simple personal server to serve audio files from directories. Intended primarily for audio books, but anything with decent directories structure will do. Focus here is on simplicity and minimalistic design.

Simple low-level web server to serve file uploads with some shell scripting-friendly features

http_file_uploader Simple low-level web server to serve file uploads with some shell scripting-friendly features. A bridge between Web's multipart/for

Xiu - A simple and secure live media server in pure Rust (RTMP/HTTP-FLV/HLS/Relay).🦀
Xiu - A simple and secure live media server in pure Rust (RTMP/HTTP-FLV/HLS/Relay).🦀

Xiu is a simple and secure live media server written by pure Rust, it now supports popular live protocols like RTMP/HLS/HTTP-FLV (and maybe other protocols in the future), you can deploy it as a stand-alone server or a cluster using the relay feature.

Docker images for compiling static Rust binaries using musl-libc and musl-gcc, with static versions of useful C libraries. Supports openssl and diesel crates.

rust-musl-builder: Docker container for easily building static Rust binaries Source on GitHub Changelog UPDATED: Major updates in this release which m

A program written in pure Rust to query music info from mpd and display it in a notification.
A program written in pure Rust to query music info from mpd and display it in a notification.

musinfo A program written in pure Rust to query music info from mpd and display it in a notification. Note: Cover art is expected to be placed at /tmp

Watches changes in a rust project, runs test and shows friendly notification
Watches changes in a rust project, runs test and shows friendly notification

Cargo testify Automatically runs tests on your Rust project and notifies about the result. Install Install prerequisites (for Debian/Ubuntu): apt-get

Bitcoin Push Notification Service (BPNS) allows you to receive notifications of Bitcoin transactions of your non-custodial wallets on a provider of your choice, all while respecting your privacy

Bitcoin Push Notification Service (BPNS) Description Bitcoin Push Notification Service (BPNS) allows you to receive notifications of Bitcoin transacti

Matrix Bot for Bitcoin Push Notification Service

BPNS Matrix Bot Description Matrix Bot for Bitcoin Push Notification Service. Requirements Rust (1.57.0+) BPNS Server Matrix account Build cargo build

Single-operator public liveness notification

Single-operator public liveness notification

An interruption-free notification system for Linux

nofi A Rofi-driven notification manager rofi.mp4 nofi is a distraction-free notification center. While most notification daemons make immediate popups

System status notification daemon for Linux desktop environments.

panorama A system status notification daemon for Linux systems. Panorama will send notifications for various system events, and is primarily useful fo

âš¡rustygram is a minimal and blazing fast telegram notification framework for Rust
âš¡rustygram is a minimal and blazing fast telegram notification framework for Rust

âš¡rustygram âš¡rustygram is a minimal and blazing fast telegram notification framework using Rust. Abstracts away the Telegram API complexity so your app

Operator is a web server. You provide a directory and Operator serves it over HTTP.
Operator is a web server. You provide a directory and Operator serves it over HTTP.

Operator Operator is a web server. You provide a directory and Operator serves it over HTTP. It serves static files the way you'd expect, but it can a

🌟 For when you really just want to serve some files over HTTP right now!
🌟 For when you really just want to serve some files over HTTP right now!

miniserve - a CLI tool to serve files and dirs over HTTP For when you really just want to serve some files over HTTP right now! miniserve is a small,

 miniserve - a CLI tool to serve files and dirs over HTTP
miniserve - a CLI tool to serve files and dirs over HTTP

🌟 For when you really just want to serve some files over HTTP right now!

A set of tools for generating signed exchanges at serve time.

sxg-rs sxg-rs is a set of tools for generating signed exchanges at serve time: cloudflare_worker runs on Cloudflare Workers. fastly_compute runs on Fa

A rust-based command line tool to serve as a gateway for a Internet Computer replica.

icx-proxy A command line tool to serve as a gateway for a Internet Computer replica. Contributing Please follow the guidelines in the CONTRIBUTING.md

Comments
  • Guess MIME types better

    Guess MIME types better

    At the moment, serve_file hard-codes some rules for producing MIME types. Surely there's a crate that does this better. Or maybe warp's built-in static file server has this done already.

    opened by jimblandy 1
Owner
Jim Blandy
Free/Open Source hacker. Mozillian. Author, with @jorendorff and @noracodes, of O'Reilly's Programming Rust.
Jim Blandy
Temporary directory management for Rust

tempdir A Rust library for creating a temporary directory and deleting its entire contents when the directory is dropped. Documentation Deprecation No

null 132 Jan 7, 2023
Highly parallelized, blazing fast directory tree analyzer

Parallel Disk Usage (pdu) Highly parallelized, blazing fast directory tree analyzer. Description pdu is a CLI program that renders a graphical chart f

Khải 237 Dec 22, 2022
Cobra is a HITMANâ„¢: Absolution server replacement.

Cobra Cobra is a HITMAN: Absolution server replacement developed by The Peacock Project. It restores all online functionality to the game. This projec

The Peacock Project 2 Jul 13, 2023
Live Server - Launch a local network server with live reload feature for static pages

Live Server - Launch a local network server with live reload feature for static pages

Lomirus 18 Nov 30, 2022
Static Web Server - a very small and fast production-ready web server suitable to serve static web files or assets

Static Web Server (or SWS abbreviated) is a very small and fast production-ready web server suitable to serve static web files or assets.

Jose Quintana 496 Jan 2, 2023
serve a static site, single page application or just a static file with Rust

cargo-server tl;dr: Does the same as "python -m http.server" or "npx serve" but for Rust ecosystem. cargo-server helps you serve a static site, single

Raphael Amorim 18 Oct 14, 2022
Serve a static site, single page application or just a static file with Rust

cargo-server tl;dr: Does the same as "python -m http.server" or "npx serve" but for Rust ecosystem. cargo-server helps you serve a static site, single

Raphael Amorim 18 Oct 14, 2022
Bookmark Change Directory - `bcd` is a way to `cd` to directories that have been bookmarked.

bcd - Bookmark Change Directory bcd is a way to cd to directories that have been bookmarked. Installation - Contribute Installation 1: Install bookmar

Alec Brown 2 Nov 3, 2022
Mount portable directory as consistent user directory.

PortableDesktop Mount portable directory as consistent user directory. PortableDesktopCli help PortableDesktopCli [options] <Target Path> <Link Path>

Kerwin Bryant 3 May 8, 2023
Hot reload static web server for deploying mutiple static web site with version control.

SPA-SERVER It is to provide a static web http server with cache and hot reload. 中文 README Feature Built with Hyper and Warp, fast and small! SSL with

null 7 Dec 18, 2022