3.1.2 • Published 5 years ago
qrhook v3.1.2
QRHook
QRHook is a small utility for creating QR-codes that will call a given function when scanned.
Installation
The module can easily be retrieved from the npm registry by running
npm install --save qrhook
Example usage
const QRHook = require("qrhook");
const html = "<h1>Hello there!</h1>";
//Generate and log a data url using the onScan-method as a hook
QRHook(onScan, {})
.then(e => console.log(e.code))
.catch(console.error);
//Handle incoming requests (created by scans)
function onScan(req, res) {
console.log("Somebody scanned *OUR* code! ( ͡☭ ͜ʖ ͡☭)");
//The content type is automatically set to text/html, so there's no need to worry about that
res.end(html);
}
/*
When the QR-code is then scanned, the scanning browser will display the heading "Hello there!", and the server will call the onScan()-method
*/
API
QRHook(hook, opts)
- returns: <Promise<Object {code, destroy}>> Promise that resolves to an object with a data url and a destroy function, which immediately invalidates the code
hook
<Function (req, res)> The function to be called when the code is scanned. Will be called with the request and response objects provided by the HTTP server, and is thus in charge of writing to - and ending the responseopts
<Object> An object containing various optionsmaxUses
<Number> The maximum amount of uses before the code becomes invalid. Defaults to Infinityexpires
<Date> The UTC timestamp up to which the code will be valid. Defaults to InfinityuniqueOnly
<Boolean> If set to true, then a browser can only trigger the hook once. Defaults to falseaddressType
<String> Dictates what address the QR-code should point to. Can beinternal
,external
orngrok
. Defaults tongrok
ngrokOpts
<Object> The options to pass to ngrok (only applies ifaddressType
is set tongrok
)port
<Number> Forces the server to listen on a specific port