Nik Nik - 4 months ago 18
Node.js Question

How to access Angular Js data from nodejs

Hi in my angular js file, I have patient objects which contain name, number and appointment date. In my node js file, I use twilio to send a text a patient. What I want to know is how to get the number from the angular js file in order to use it in my node Js file so I can send the patient a text. Thanks.

here is the part of server.js where I send the text message

app.post('/testtwilio', function(req,res){

var cheerio = require('cheerio'),
$ = cheerio.load('file.html'),
fs = require('fs');
fs.readFile('./views/index.html', function (err, html) {
if (err) {
throw err;
} else {
$ = cheerio.load(html.toString());
console.log($scope.patients)//$('.reminder').attr('text'));
}
});
client.sendMessage({
to: '{{patient.number}}',
from: '+16173935460',
body: 'Text sent using NodeJS'
}, function(err, data){
if(err)
console.log(err);

});
})


Here is the patient object in the MainController.js

$scope.patients = [
{
name: 'John Smith',
date: "12/22/2016",
number: 1829191844
},
{
name: 'Matt',
date: "09/15/2016",
number: 1829198344
},
{
name: 'John',
date: "08/25/2016",
number: 1829198844
},


];

Answer

Pass the data from the front end to the backend? Have angular call a route on your backend with the data you need and access it with the req.params object.

Is your server.js a node.js controller?

In the angular controller you could import $http and just do a $http.post({myparams}, /myRoute, function(results){console.log(results)})

Like Mike says. The only way to really share files between the front end and the backend is if the files are JSON as that can be read by Angular and Node. However, I usually just use this for static configuration files.

Comments