简体   繁体   中英

How to call simple Node.js script from Javascript?

I'm trying to create a basic web application with SMS capability. However, I'm a bit stumped. I'm using Twilio's SMS service, but it utilizes Node.js. Obviously, you can run it with a terminal command such as "node send_text.js", but I'm trying to make the call to the send_text.js file without using the terminal command.

I understand that you can use Express to host your web application, but I'm not too sure how you would go about calling a Node.js file from javascript.

The following is the send_text.js file I would like to call from Javascript.

var twilio = require('twilio');

// Find your account sid and auth token in your Twilio account Console.
var client = new twilio('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN');

// Send the text message.
client.messages.create({
  to: 'YOUR_NUMBER',
  from: 'YOUR_TWILIO_NUMBER',
  body: 'Hello from Twilio!'
});

Essentially, I'll detect a change in data using Javascript, and when the change is detected, call the send_text.js file to send a text to the user.

EDIT:

I've tried my own basic implementation of using require, but it doesn't seem to work properly.

Here's what I have in my index.html file:

<script type="text/javascript" src="send_text.js"></script>
<input type="button" onclick="sendText()" value="run external javascript">

And when I try to call the send_text.js, which has the following, I do not get any text message sent:

function sendText()
{
var twilio = require('twilio');

// Find your account sid and auth token in your Twilio account Console.
var client = new twilio('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN');

// Send the text message.
client.messages.create({
  to: 'YOUR_NUMBER',
  from: 'YOUR_TWILIO_NUMBER',
  body: 'Hello from Twilio!'
});
}

However, the code does work if send_text.js has the following:

function sendText()
{
    alert("Hello world")
}

You can use the child process in Nodejs to call a js script from another js file using childProcess.fork(filepath); according to the question I understood. should look something like this

let childProcess = require('child_process');
childProcess.fork('./send_text.js);

for more information see

https://nodejs.org/api/child_process.html

The code you're showing us is some example source code. You can copy and paste the relevant parts directly into your Express app.

Twilio developer evangelist here.

You could use a tool like Netlify--here's a tutorial on how to send text messages from a static website using Twilio, Netlify, and Serverless Functions .

You could also use Twilio's serverless environment for web apps called Twilio Functions -- here's a tutorial on that using the Twilio CLI as well.

Similarly, you couldsend a SMS from a Gatsby website with Serverless Functions and React.js , or use an AWS Lambda Function to send a SMS .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM