-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
139 lines (122 loc) · 3.47 KB
/
Copy pathserver.js
File metadata and controls
139 lines (122 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//Lets require/import the HTTP module
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var mongoURL = 'mongodb://localhost:27017/test';
var fs = require('fs');
var http = require('http');
var url = require('url') ;
var util = require("util");
//Lets define a port we want to listen to
const PORT=4040;
var itemsProcessed = 0;
var total =0;
var queryData;
MongoClient.connect(mongoURL, function(err, db) {
db.ensureIndex("tweets", {
document: "text"
}, function(err, indexname) {
assert.equal(null, err);
});
});
//We need a function which handles requests and send response
function handleRequest(request, response){
queryData = url.parse(request.url, true).query;
MongoClient.connect(mongoURL, function(err, db) {
assert.equal(null, err);
console.log(queryData.page);
if(queryData.page =="stream"){
findTweetsStream(db, writeHTML, response);
}
else if(queryData.page =="find"){
findTweets(db, writeHTML, response);
}
else{
showStats(db, writeHTML, response);
}
});
};
var writeHTML = function(html,response,db){
response.writeHead(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
db.close();
}
var showStats = function(db, callback,res) {
var html = '';
db.collection('tweets').count(function(err, count){
html += '<h2> Document Count </h2>';
html += '<p>'+JSON.stringify(count)+'</p>';
db.stats(function(err, stats){
html += '<h2> Stats </h2>';
html += '<p>'+JSON.stringify(stats)+'</p>';
callback(html,res,db);
});
});
};
var findTweetsStream = function(db, callback,res) {
var cursor =db.collection('tweets').find();
var html = '<h2> Results '+queryData.search+' </h2>';
var counter=0;
cursor.on('data', function(tweet) {
if (tweet != null) {
console.log(counter++);
//console.dir(tweet);
if(tweet.name !=null){
html += '<p><b>Name:</b> ';
html += tweet.user.name;
}
if(tweet.text !=null){
html += ' <br /><b>Text:</b> '
html += tweet.text;
}
}
});
cursor.once('end', function() {
callback(html,res,db);
});
};
var findTweets = function(db, callback,res) {
var cursor =db.collection('tweets').find();
var html = '<h2> Results '+queryData.search+' </h2>';
cursor.each(function(err, tweet) {
assert.equal(err, null);
if (tweet != null) {
//console.dir(tweet);
html += '<p><b>Name:</b> '
+ tweet.user.name
+ ' <br /><b>Text:</b> '
+ tweet.text;
} else {
callback(html,res,db);
}
});
};
var searchTweets = function(db, callback,res) {
var cursor = db.collection('tweets').find({
"$text": {
"$search": "tory"
}
});
var html = '<h2> Search Results '+queryData.search +' </h2>';
cursor.each(function(err, tweet) {
assert.equal(err, null);
if (tweet != null) {
//console.dir(tweet);
html += '<p><b>Name:</b> '
+ tweet.user.name
+ ' <br /><b>Text:</b> '
+ tweet.text;
} else {
callback(html,res,db);
}
});
};
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});