I defined these three routes in app.js
app.use('/', require('./routes/index'));
app.use('/LEDon', require('./routes/LEDon'));
app.use('/LEDoff', require('./routes/LEDoff'));
In my route file I have the following:
var express = require('express');
var router = express.Router();
var Gpio = require('onoff').Gpio,
led = new Gpio(17, 'out');
router.get('/', function(req, res, next) {
led.writeSync(1);
});
module.exports = router;
So when I go to the /LEDon page the method runs and everything works. Is it possible though to run a method without using a get request? My main goal is to just click a hyperlink which then runs the method..
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 isSocket.io
This similar stackoverflow question should help you out.
edit: I made a simple example spanning multiple files:
/test/app.js:
/test/clientside.js
/test/view.html
To run it:
node app.js
in terminal, and openview.html
on your browser. Try pressing the button and check out your terminal. Hope this helps.For resolve your problem you can use
ajax
request, for example: