简体   繁体   中英

Send/receive data from Javascript to Node.js using Express

I am currently working with the Express platform, the Twilio Node.js SMS API and obviously javascript to send text messages to my users. Problem is, I don't know what I should do in order to send data through my GET variables on the front-end and capture those values with node.js on the back-end.

For testing purposes, I created a simple button that sends a text message to a fixed number when clicked.

Here is the javascript side:

 function sms() { xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET","http://localhost:5001", true); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ alert(xmlhttp.responseText); } } xmlhttp.send(); } 

Here is the node.js side:

 var accountSid = 'ACCOUNT_SID'; var authToken = 'ACCOUNT_TOKEN'; //require the Twilio module and create a REST client var client = require('twilio')(accountSid, authToken); var express = require("express"); var app = express(); app.get('/',function(request,response){ var to = "TO"; var from = "FROM"; client.messages.create({ to: to, from: from, body: 'Another message from Twilio!', }, function (err, message) { console.log("message sent"); }); }); app.listen(5001); 

I have came across two ways to send a responseText from Node.js, but can't manage to make them work first one using response.send("Hello World"); or the second one response.write("Hello again"); response.end(); response.write("Hello again"); response.end();

So just to sum it up, I want to send variables (to, from, message, etc.) through my http request, capture them in node.js and send a responseText! As a heads up, I'm very comfortable with AJAX requests between JS and PHP, but Node.js is new to me.

Thanks in advance

I think the new Guides will help you out here with How to receive and reply to SMS in Node.js:

https://www.twilio.com/docs/guides/sms/how-to-receive-and-reply-in-node-js

The CodeRail along the right hand side will walk you through it step-by-step but you should pay attention particularly to the section titled "Generate a dynamic TwiML Message".

var http = require('http'),
    express = require('express'),
    twilio = require('twilio'),
    bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.urlencoded({ extended: true })); 

app.post('/', function(req, res) {
    var twilio = require('twilio');
    var twiml = new twilio.TwimlResponse();
    if (req.body.Body == 'hello') {
        twiml.message('Hi!');
    } else if(req.body.Body == 'bye') {
        twiml.message('Goodbye');
    } else {
        twiml.message('No Body param match, Twilio sends this in the request to your server.');
    }
    res.writeHead(200, {'Content-Type': 'text/xml'});
    res.end(twiml.toString());
});

http.createServer(app).listen(1337, function () {
    console.log("Express server listening on port 1337");
});

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