-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (43 loc) · 1.48 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
var express = require('express'),
fs = require('fs'),
mongodb = require('mongodb');
var app = express(),
peerList = {},
webPort = process.env.PORT || 2010,
DBuri = 'mongodb://mainUser:monkeys99@ds061228.mongolab.com:61228/newswatch';
app.use(express.static(process.cwd() + '/public'));
app.on("render:index", function(encoding, req, res) {
fs.readFile(__dirname+"/views/index.html", encoding, function(err, html) {
res.contentType("text/html");
res.send(200, html);
});
});
app.get("/", function(req, res, next) {
app.emit("render:index", "UTF-8", req, res);
});
app.on("render:about", function(encoding, req, res) {
fs.readFile(__dirname+"/views/about.html", encoding, function(err, html) {
res.contentType("text/html");
res.send(200, html);
});
});
app.get("/about", function(req, res, next) {
app.emit("render:about", "UTF-8", req, res);
});
app.on("render:headLineData", function(encoding, req, res, data) {
res.contentType("application/json");
res.send(200, JSON.stringify(data));
});
app.on("get:headLineData", function(encoding, req, res) {
mongodb.MongoClient.connect(DBuri, function(err, db) {
if(err) throw err;
var newsSources = db.collection('newsSources');
newsSources.find({name : "BBC UK"}).limit(1).toArray(function(err, doc) {
app.emit("render:headLineData", "UTF-8", req, res, doc);
});
});
});
app.get("/getHeadlineData", function(req, res, next) {
app.emit("get:headLineData", "UTF-8", req, res);
});
app.listen(webPort);