ajax - How to use NodeJS with node-rest-client methods to post dynamic data to front end HTML -


i rather new nodejs able articulate question(s) properly. goal create nodejs application use node-rest-client data , asynchronously display in html on client side.

i have several node-rest-client methods created , calling data operation when user navigates /getdata page. response logged console i'm stumbling on best method dynamically populate data in html table on /getdata page itself. i'd follow node best practices, ensure durability under high user load , make sure i'm not coding piece of junk.

  • how can bind data returned express routes html front end?
  • should use separate "router.get" routes each node-rest-method?
  • how can bind request button , have new data when clicked?
  • should consider using socket.io, angularjs , ajax pipe data server side client side?

-thank reading.

this example of route rendering getdata page calling getdomains node-rest-client method. page rendering correct , data returned getdomains printed console, i'm having trouble getting data piped /getdata page.

router.get('/getdata', function(req, res) {     res.render('getdata', {title: 'this data page'});      console.log("rendering:: starting post requirement");             args =  {             headers:{"cookie":req.session.qcsession,"accept":"application/xml"},             };      qcclient.methods.getdomains(args, function(data, response){          var theprojectsstring = json.stringify(data);         var theprojectsjson = json.parse(theprojectsstring);          console.log('processing json.stringify on data');                    console.log(theprojectsstring);         console.log('processing json.parse on theprojectsstring');         console.log('');          console.log('parsing array  ' + theprojectsjson.domains.domain[0].$.name );      });      }); 

i've started experiment creating several routes different node-rest-client methods use res.send return data , perhaps bind ajax call or use angularjs parse data , display user.

router.get('/domaindata', function(req, res){              var theprojectsstring;             var theprojectsjson;              args =  {                         headers:{"cookie": req.session.qcsession,"accept":"application/xml"},                     };              qcclient.methods.getdomains(args, function(data, response){                 //console.log(data);                  theprojectsstring = json.stringify(data);                 theprojectsjson = json.parse(theprojectsstring);                  console.log('processing json.stringify on data');                                console.log(theprojectsstring);                              console.log('processing json.parse on theprojectsstring');                 console.log('');                  console.log('parsing array  ' + theprojectsjson.domains.domain[0].$.name );                  res.send(theprojectsstring);             });          }); 

i looked code. using res.render(..) , res.send(..). first of should understand basic request-response cycle. request object gives values passed routes, , response return values after doing kind of processing on request values. more particularly in express using req.params , req.body if values passed through body of html.

so response related statements(res.send(..),res.json(..), res.jsonp(..), res.render(..)) should @ end of function(req,res){...} have no other processing left done, otherwise errors.

as per modern web application development practices in javascript, frameworks such ruby on rails, expressjs, django, play etc work rest engine , front end routing logic written in javascript. if using angularjs ngroute , open source ui-router makes work easy. if closely of popular mean seed projects such mean.io, mean.js use expressjs rest engine , angularjs rest of heavyweight job in front end.

very sending json data backend can use res.json(..). consume data endpoints can use angularjs ngresource service.

let's take simplest case, have /domaindata end point :

router.get('/domaindata',function(req,res){ .. .. res.json({somekey:'somevalue'}); }); 

in front end can access using angularjs ngresource service :

var myresource = $resource('/domaindata'); myresource.query(function(results){     $scope.myvalue = results;      //myvalue variable bonded view. }); 

i suggest have ui-router ui front end routing.

if looking sample implementation can this project wrote sometime back, can give overview of implementing login, session management using json web token.

there lot of things understand, let me know if need in anything.


Popular posts from this blog