@jamen/http-reload
An autoreload interface that works purely over HTTP.
Install
npm i @jamen/http-reload
Usage
createReloader(options)
Create a reloader. The available options are:
-
pollUrl
: The URL for the request that indicates when to update. Defaults to/__request_poll
-
scriptUrl
: The URL of the script which polls for the update. Defaults to/__request.js
-
reloadScript
: Script file which gets used on the front-end. Defaults to an internal script, see lib/inject.js
let reloader = createReloader({
pollUrl: '/__request_poll',
scriptUrl: '/__request.js',
reloadScript: './lib/inject.js'
})
reloader.handlePoll(request, response)
Handles a poll request (associated with options.pollUrl
). Returns true if the request was a poll and was handled, otherwise false.
createServer((request, response) => {
// ...
if (reloader.handlePoll(request, response)) {
return // work passed off to reloader. quit
}
})
reloader.handleScript(request, response)
Handles a poll script request (associated with options.scriptUrl
). Returns true if the request was for the script and was handled, otherwise false.
createServer((request, response) => {
// ...
if (reloader.handleScript(request, response)) {
return // work passed off to reloader. quit
}
})
reloader.transformHtml()
Transform an HTML stream so that it uses the reload script.
fs.createReadStream('/tmp/foo.html')
.pipe(reloader.transformHtml())
.pipe(response)
Also see @jamen/http-static
reloader.send(message, status?)
Send a reload or error message to all polling connections. The status is an HTTP status code that defaults to 200
.
watcher(inputDir, file => {
reloader.send(message)
})
readdir('/tmp/foo.txt', err => {
reloader.send(err, 500)
})