Speed up development with full-stack environments for every branch.

Learn More

How to use Namespaces with socket.io and node.js

646 Runs 6691 Views 275 Copies
Saved

Saved

sundip 66

sundip
published 5 years ago

var request = require('request')//HTTP request client
  , express = require('express')//web dev framework
  , qs = require('qs')          //querystring parser
  , app = express();


var screen_name = "getrunnable"; // replace with your screenname



var output = "<h1>@" + screen_name + "'s friends</h1>"
  , getFriendsURL = "http://api.twitter.com/1/friends/ids.json?"
      + qs.stringify( { screen_name: screen_name } );

// send request to get list of friends
request.get(getFriendsURL, function (err, res, friends) {
  friends = JSON.parse(friends);
  // only iterate through at most 5 friends
  var len = (friends.ids.length>5) ? 5 : friends.ids.length; 

  // loop through friends (the first 5 in the list)
  for(var i=0; i<len; i++) {
    // set the params to get user details

    var getUserDetailsURL = 'https://api.twitter.com/1/users/show.json?' 
          + qs.stringify({user_id: friends.ids[i]});

    //send request to get details for each friend
    request.get(getUserDetailsURL, function (err, res, friend) {
      friend = JSON.parse(friend);
      //create list of friends profile pic and screen name
      output += '<img src="' + friend.profile_image_url + '" title="@' + friend.screen_name + '" />'
    });
  }
});

// attach stylesheet to output
app.use(express.static(__dirname));
output = '<link rel="stylesheet" href="styles.css" />' + output;

app.get('/', function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(output); //send output 
}).listen(80);

console.log('http server started on port: '+ 80);
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).