Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling method in Node js from browser (Using Express)

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

like image 821
user2255273 Avatar asked Feb 14 '15 15:02

user2255273


People also ask

How do you call a method in NodeJS?

The call() method returns the result of calling the functionName() . By default, the this inside the function is set to the global object i.e., window in the web browsers and global in Node. js. Note that in the strict mode, the this inside the function is set to undefined instead of the global object.

What is Express () in NodeJS?

Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.

Can browser run Express?

You can, however, run the express server as a separate process and use AJAX to communicate with it and run your scripts. This can be done through various of front end libraries. Some examples are Axios and RequestJS (I have used both succesfully to communicate a browser with an express server).


2 Answers

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>
like image 90
siavolt Avatar answered Sep 27 '22 19:09

siavolt


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.

like image 41
Keith Yong Avatar answered Sep 27 '22 18:09

Keith Yong