Skip to content

Instantly share code, notes, and snippets.

@evanemolo
Created April 17, 2012 13:08
Show Gist options
  • Save evanemolo/2405864 to your computer and use it in GitHub Desktop.
Save evanemolo/2405864 to your computer and use it in GitHub Desktop.
Tweet Count Web App
var express = require('express');
var ejs = require('ejs'); //embedded javascript template engine
var app = express.createServer(express.logger());
var mongoose = require('mongoose'); // include Mongoose MongoDB library
var schema = mongoose.Schema;
var requestURL = require('request');
/************ DATABASE CONFIGURATION **********/
//app.db = mongoose.connect(process.env.MONGOLAB_URI); //connect to the mongolabs database - local server uses .env file
// Include models.js - this file includes the database schema and defines the models used
require('./models').configureSchema(schema, mongoose);
// Define your DB Model variables
var BlogPost = mongoose.model('BlogPost');
var Comment = mongoose.model('Comment');
/************* END DATABASE CONFIGURATION *********/
/*********** SERVER CONFIGURATION *****************/
app.configure(function() {
/*********************************************************************************
Configure the template engine
We will use EJS (Embedded JavaScript) https://github.com/visionmedia/ejs
Using templates keeps your logic and code separate from your HTML.
We will render the html templates as needed by passing in the necessary data.
*********************************************************************************/
app.set('view engine','ejs'); // use the EJS node module
app.set('views',__dirname+ '/views'); // use /views as template directory
app.set('view options',{layout:true}); // use /views/layout.html to manage your main header/footer wrapping template
app.set( "jsonp callback", true );
app.register('html',require('ejs')); //use .html files in /views
/******************************************************************
The /static folder will hold all css, js and image assets.
These files are static meaning they will not be used by
NodeJS directly.
In your html template you will reference these assets
as yourdomain.heroku.com/img/cats.gif or yourdomain.heroku.com/js/script.js
******************************************************************/
app.use(express.static(__dirname + '/static'));
//parse any http form post
app.use(express.bodyParser());
/**** Turn on some debugging tools ****/
app.use(express.logger());
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
/*********** END SERVER CONFIGURATION *****************/
app.get('/', function(request, response) {
console.log('web app fired.');
// Define the remote JSON feed
twitterSearchUrl = "http://search.twitter.com/search.json?q=rain&nearNYCwithin1&rpp100&page1";
// Make the request
requestURL(twitterSearchUrl, function(error, results, data) {
// If there is an error
if (error) {
console.log('There is an error: ' + error);
response.send('There is an error');
// If connected to JSON feed
} else if (results.length > 0) {
console.log('Successful response.');
// Convert JSON into JS
var twitterData = JSON.parse(data);
console.log('Parsing JSON data');
if (twitterData.results.length > 0) {
console.log('Counting tweets.');
var tweetCount = [];
for (var i=0; i < results.length; i++) {
results.push(results[i]);
console.log('Return tweet count: ' + results[i]);
}
} else {
response.send('Twitter JSON feed status != OK');
}
}
});
});
// Make server turn on and listen at defined PORT (or port 3000 if is not defined)
var port = process.env.PORT || 3000;
app.listen(port, function() {
console.log('Listening on ' + port);
});
@astulock
Copy link

Excuse me if I am just being a n00b, but after reading the code comments I cannot determine how to implement this. Is this code intended for use on websites in order to display tweet counts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment