Calling method in Node js from browser (Using Express)
For resolve your problem you can use ajax
request, for example:
<body>
<a onClick=LEDon>LED On</a>
<a onClick=LEDoff>LED Off</a>
<script>
function LEDon(){
$.ajax({
url: "http://yourDomain.com/LEDon"
});
}
function LEDoff(){
$.ajax({
url: "http://yourDomain.com/LEDoff"
});
}
</script>
<body>
Essentially you are asking your client side script to directly call a function on your Node server script. The only other choice other than an Ajax POST
AFAIK is Socket.io
This similar stackoverflow question should help you out.
edit: I made a simple example spanning multiple files:
/test/app.js:
var express = require('express');
var app = express();
app.post('/LEDon', function(req, res) {
console.log('LEDon button pressed!');
// Run your LED toggling code here
});
app.listen(1337);
/test/clientside.js
$('#ledon-button').click(function() {
$.ajax({
type: 'POST',
url: 'http://localhost:1337/LEDon'
});
});
/test/view.html
<!DOCTYPE html>
<head>
</head>
<body>
<button id='ledon-button'>LED on</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src='clientside.js'></script>
</body>
To run it: node app.js
in terminal, and open view.html
on your browser. Try pressing the button and check out your terminal. Hope this helps.