0

please can someone explain how I can made this nodejs script run when a button is clicked?

var Twit = require('twit');
var t = new Twit({
consumer_key: '******',
consumer_secret: '******',
access_token: '******',
access_token_secret: '******',
});

t.post('statuses/update', {status:'hello world'},function(err, data, response){
console.log(data);
});

Currently I get:

Uncaught ReferenceError: require is not defined

Thanks

2 Answers 2

4

You will need to have a NodeJS process running as a server in which a callback can be hooked to an API endpoint which will then be run when you click on a button in a website.

  1. Client gets HTML with button (from a webserver of any type)
  2. Client clicks on button
  3. HTTP request is sent to NodeJS server at a particular url (yourip.com/yourscript)
  4. NodeJS server calls a function that you assigned to that particular URL
Sign up to request clarification or add additional context in comments.

Comments

2

NodeJS is used on the server-side, and can be described as an extension of JavaScript which can do more stuff, like file operations.

Normal JavaScript is used on the client-side.

Therefore, you cannot just use nodeJS in the browser, because the browser is lacking features that nodeJS offers. What you can do is use nodeJS to build an API and connect the API to the client-side script. You will need a server for this to work, though.

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.