-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
88 lines (66 loc) · 2.09 KB
/
app.js
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
// http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#mongoclient-connection-pooling
var express = require('express'),
bodyParser = require("body-parser"),
MongoClient = require('mongodb').MongoClient;
var MONGO_URL = "mongodb://localhost:27017/",
PORT = 3000,
DB_NAME = 'Venom',
COLLECTION_NAME = 'Tweets',
CURSOR_QUERY_OBJ = {};
var db,
cursor,
prevDocID;
app = express();
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/javascripts'))
MongoClient.connect(MONGO_URL + DB_NAME, function(err, database) {
if(err) throw err;
db = database;
cursor = db.collection(COLLECTION_NAME).find(CURSOR_QUERY_OBJ);
app.listen(PORT);
console.log("Listening on port " + PORT);
});
app.get("/", function(req, res) {
cursor.nextObject(function (err, doc) {
if (doc) {
prevDocID = doc['_id'];
res.render('index', { tweet: doc['text'] });
} else {
db.close();
res.end("No Mas.");
}
})
});
app.post("/applyLabel", function(req, res) {
var labelName = req.body['labelName'],
labelValue = req.body['labelValue'],
resObj = Object.create(null);
if (labelName) {
updatePreviousObject(labelName, labelValue);
resObj['message'] = "\n\tUpdated tweet with Mongo _id = " + prevDocID + "; set field '" + labelName + "' to '" + labelValue + "'";
} else {
resObj['message'] = "Previous tweet was not labeled.";
}
cursor.nextObject(function (err, doc) {
if (doc) {
prevDocID = doc['_id'];
resObj['tweet'] = doc['text'];
res.end(JSON.stringify(resObj));
} else {
db.close();
res.end(resObj);
}
})
});
function updatePreviousObject (labelName, labelValue) {
if(labelName && prevDocID) {
var updateObject = Object.create(null);
updateObject[labelName] = labelValue;
db.collection(COLLECTION_NAME).update(
{ _id: prevDocID },
{ $set: updateObject },
function (err, result) { if (err) console.error(err); }
);
}
}