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