-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticServer.js
More file actions
115 lines (109 loc) · 4.11 KB
/
staticServer.js
File metadata and controls
115 lines (109 loc) · 4.11 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
var fs = require('fs');
var http = require('http');
var https = require('https');
var url = require('url');
var debug = require('debug');
var assert = require('assert');
var express = require('express'), jade = require('jade'),
ejs = require('ejs'), bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
var ROOT_DIR = "/home/ec2-user/public_html";
var options = {
hostname: 'localhost',
host: '127.0.0.1',
key: fs.readFileSync('ssl/server.key'),
cert: fs.readFileSync('ssl/server.crt')
};
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
app.get('/',function(req, res) {
res.sendFile(ROOT_DIR+"/index.html");
});
app.use('/', express.static(ROOT_DIR, {maxAge: 60*60*1000}));
var basicAuth = require('basic-auth-connect');
var auth = basicAuth(function(user, pass) {
return((user ==='cs360')&&(pass === 'test'));
});
app.get('/getcity', function (req, res) {
var urlObj = url.parse(req.url, true, false);
var myRe = new RegExp("^"+urlObj.query["q"]);
fs.readFile('cities.dat.txt', function (err, data) {
if(err) throw err;
cities = data.toString().split("\n");
var citiesFiltered=[];
for(var i = 0; i < cities.length; i++) {
var result = cities[i].search(myRe);
if(result != -1) {
citiesFiltered.push({city:cities[i]});
}
}
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(citiesFiltered));
});
});
app.get('/comment', function (req, res) {
console.log("In comment route");
var MongoClient = require('mongodb').MongoClient;
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
MongoClient.connect("mongodb://localhost", function(err, db) {
if(err) { console.log("Failed connection"); throw err; }
db.authenticate("dbuser","1h@v3power",function(err, authRes) {
if(err) { console.log("Failed authentication"); throw err; }
var weatherDb = db.db("weather");
assert(!null,weatherDb);
weatherDb.collection('comments').find(function(err, items) {
if(err) { console.log("Failed read"); throw err; }
items.toArray(function(err, itemArr){
console.log("Document Array: ");
console.log(itemArr);
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(itemArr));
});
});
});
});
});
app.post('/comment', auth, function (req, res) {
console.log("In POST comment route");
console.log(req.body);
var jsonData = "";
req.on('data', function (chunk) {
jsonData += chunk;
});
req.on('end', function () {
var reqObj = JSON.parse(jsonData);
console.log(reqObj);
// Now put it into the database
var MongoClient = require('mongodb').MongoClient;
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
console.log("Before connecting");
MongoClient.connect("mongodb://localhost", function(err, db) {
if(err) { console.log("Failed connection"); throw err; }
console.log("connected");
db.authenticate("dbuser","1h@v3power",function(err, authRes) {
if(err) { console.log("Failed authentication"); throw err; }
console.log("authenticated");
var weatherDb = db.db("weather");
assert(!null,weatherDb);
weatherDb.collection('comments').insert(reqObj,function(err, records) {
if(err) { console.log("Failed insert"); throw err; }
console.log("Record added as "+records[0]._id);
});
});
});
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end("");
});
});