11

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..

3 Answers 3

15

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Would you be willing to show me how to use Socket.io when clicking on a hyperlink? I have been looking at Socket.io before but just can't figure it out..
Sorry I am not familiar with Socket.io. $.ajax would probably be easier. The other answer has a great Ajax answer
Thanks! I ended up using socket.io, which works great. So thanks for pointing me in the right direction.
This worked for me! I'm making a telepresence robot and already have the remote controls listening on a UDP port for drive commands (that eventually sends it over Serial to the iRobot Create). So with this I was able to put W A S D keys on my web page and call the server side node.js function to send a UDP message to that command server! Thanks!
Can't get this to work on Windows Server. View.html displays 'Cannot GET /view.htm'
1

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>

Comments

0

Old question - I know. But as time flies, new ways appear.

Can I suggest trying api-mount. It basically allows calling API as simple functions without having to think about AJAX requests, fetch, express, etc. Basically in server you do:

const ApiMount = apiMountFactory()
ApiMount.exposeApi(api)

"api" is basically an object of methods/functions that you are willing to call from your web application.

On the web application you then do this:

const api = mountApi({baseUrl: 'http://your-server.com:3000'})

Having done that you can call your API simply like this:

const result = await api.yourApiMethod()

Try it out. Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.