Advertisement
taufikmas

request http rest api nodejs

Jun 20th, 2017
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http');
  2.  
  3. function getJSON(options,cb){
  4.     http.request(options,function(res){
  5.     var body ='';
  6.     res.on('data', function(chunk){
  7.         body+=chunk;
  8.     });
  9.  
  10.     res.on('end',function(){
  11.         var hasil = JSON.parse(body);
  12.         cb(null, hasil);
  13.     })
  14.     res.on('error', cb);
  15.  
  16.     })
  17.     .on('error',cb)
  18.     .end();
  19.  
  20. }
  21.  
  22. var options = {
  23.     host:'pubsub.pubnub.com',
  24.     port:80,
  25.     path:'/time/0',
  26.     method: 'GET'
  27. };
  28.  
  29.  
  30. getJSON(options, function(err,hasil){
  31.     if (err){
  32.         return console.log('error dapetin data', err);
  33.     }else {
  34.         console.log('hasile ndoorr', hasil);
  35.  
  36.         // setTimeout(function() {
  37.         //        getJSON(options,cb);
  38.         //    }, 5000);
  39.     }
  40.  
  41.  
  42.  
  43. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement