0.5.0 • Published 7 years ago
static-reload v0.5.0
static-reload
Small static server that reloads on demand.
static-reload start
# In another process:
static-reload
See Usage for info about using it as a js module.
Install
npm i -D static-reload
Usage
Command-line interface
Start a server first using:
static-reload start [entry] [port]
And reload that server using:
static-reload [port]
Arguments available are:
entry
where files are served statically. Defaults to CWD.port
where server is started locally. Defaults to3000
.
reloader.start(server, options)
Takes a http.Server
and entry
and adds the static and reloading handles.
The options are:
entry
is a path where files are served statically.onreload()
when a reload event is received by a tool.oninject(file)
when a script injected into html file on request.
const server = http.createServer()
reloader.start(serve, {
entry: __dirname + '/dist',
onreload () {
console.log('Reloaded')
},
oninject (file) {
console.log(`Enabled ${file}`)
}
})
server.listen(3000)
It works by enabling WebSockets on the server and injecting a 1-line script into any HTML files requested. The script connects to the WebSocket server and waits for a message to reload the page. Then, when the websocket server is messaged (by a tool) it messages all clients back, and your browser tabs reload.
reloader.reload(server)
reloader.reload(options)
Reloads pages with http.Server
or { address, port }
after having done reloader.start
.
reloader.reload(server)
reloader.reload({ address: 'localhost', port: 3000 })