I'm trying to use AJAX to call on a Node.js file to send a text message on button press. So when I type node app.js in the terminal the message sends to my phone, however, when I use a jQuery click event in my javascript file, it's no longer sending within the browser. I'm thinking the primary problem is within my index.js file and not the Node.js file, more specific, the actual URL path.
Here's my app.js file that contains the info to send the text message (app.js):
var accountSid = process.env.TWILIO_ACCOUNT_SID;
var authToken = process.env.TWILIO_AUTH_TOKEN;
var client = require('twilio')(accountSid, authToken);
var express = require('express');
var app = express();
client.messages.create({
to: "+1...",
from: "+1...",
body: "Testing, testing, testing"
}).then((messsage) => console.log(message.sid));
My file that contains Javascript (index.js):
$("#buttons").click(function() {
alert("Clicked.");
$.ajax({
url: "/app.js",
type: "POST",
beforeSend: function() {
alert("testing");
},
success: function(result) {
// $("#result").html(result);
alert(" sss ");
}
}).error(function() {
alert("wrong");
});
});
So my primary question is, is the URL suppose to be the location/name of the file you're trying to run? So far that's been my understanding, yet the message isn't being sent.
This isn't going to work. Your ajax request is going to be looking for a service that speaks http and running environment to execute your code, which you haven't provided.
It's not hard though since you already have Express installed.
Here's a bare-bones node/express setup:
var accountSid = process.env.TWILIO_ACCOUNT_SID;
var authToken = process.env.TWILIO_AUTH_TOKEN;
var client = require('twilio')(accountSid, authToken);
const express = require('express')
const app = express()
app.post('/api', function (req, res) {
client.messages.create({
to: "+1...",
from: "+1...",
body: "Testing, testing, testing"
}).then((messsage) => {
console.log(message.sid)
res.send('done!', message.sid )
});
})
app.listen(8000, function () {
console.log('Example app listening on port 8000!')
})
Now you should be able to access it with something like:
$.ajax({
url: "http://localhost:8000/api",
// etc.
})
I believe you misunderstand what happens when you run the ajax call. Think of it like going to a URL in your browser. If you go to something like localhost:8080/app.js (assuming you have a server running) you will get the text you posted for app.js, there is no way for the code to run on the server.
Instead, you need a server listening for a URL like localhost:8080/runappjs. When the receives this url have the code from app.js run, then return something to the browser. This could be JSON, HTML, or whatever you want and could indicate status.
Here is a simple example using express routing
app.post('/runappjs', function (req, res) {
***PUT APPJS CODE HERE FOR SIMPLICITY YOU CAN REFACTOR LATER***
var accountSid = process.env.TWILIO_ACCOUNT_SID;
var authToken = process.env.TWILIO_AUTH_TOKEN;
var client = require('twilio')(accountSid, authToken);
var express = require('express');
var app = express();
client.messages.create({
to: "+1...",
from: "+1...",
body: "Testing, testing, testing"
}).then((messsage) => console.log(message.sid));
res.send('Got a POST request')
})