Socket
Book a DemoInstallSign in
Socket

expose-js

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expose-js

Expose JavaScript functions, and objects on server to the browser.

latest
Source
npmnpm
Version
0.0.2
Version published
Maintainers
1
Created
Source

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>

FAQs

Package last updated on 28 Apr 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts