expose-js
v0.0.2
Published
Expose JavaScript functions, and objects on server to the browser.
Downloads
1
Readme
Example
This example can be found in examples/simple-function.
app.js
const express = require("express")
, expose = require("expose-js")
, fs = require("fs");
var clickCount = 0;
expose.function("getMessage", function() {
this.callback("This button has been clicked " + ++clickCount + " times.");
});
var app = express();
// use expose to handle exposed function calls and object getters/setters
app.use(expose);
// serve expose script to allow us to use exposed functions and objects
app.get("/server.js", expose.script("server"));
// serve app.html
app.get("/", function(req, res) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(fs.readFileSync("app.html"));
});
// redirect any other traffic to /
app.use(function(req, res) {
res.redirect("/");
});
app.listen(3000);
app.html
<button id="btn">Click me</button>
<script src="/server.js"></script>
<script>
(function() {
var btn = document.getElementById("btn");
btn.onclick = function(e) {
if(btn.disabled) return;
btn.disabled = true;
server.getMessage(function(msg) {
btn.textContent = msg;
btn.disabled = false;
});
};
}());
</script>