0

Is there a way to detect a button press with Node.JS? I want to run a function when a certain button is pressed. If possible, preferably without Socket.IO.

1
  • Node.JS is server-side, you'll need an AJAX call to run node code on click. Commented Aug 5, 2014 at 17:10

1 Answer 1

2

clientside:

$('button').click(function () {
  $.post('/thing', {data: 'blah'}, function (data) {
    console.log(data);
  });
}, 'json');

serverside:

var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.urlEncoded());
app.post('/thing', function (req, res, next) {
  var data = myFunction(req.body);
  res.json(data);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Works, but it only works about 6 times then you have to refresh.

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.