-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpServ.js
More file actions
107 lines (107 loc) · 4.79 KB
/
expServ.js
File metadata and controls
107 lines (107 loc) · 4.79 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
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var url = require('url');
var app = express();
var bodyParser = require('body-parser');
var basicAuth = require('basic-auth-connect');
var auth = basicAuth(function(user, pass) {
return((user ==='cs360')&&(pass === 'test'));
});
app.use(bodyParser());
var options = {
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.send("Get Index");
});
app.use('/', express.static('./html', {maxAge: 60*60*10}));
app.get('/getcity', function (req, res) {
//console.log("In getcity route");
//res.json([{city:"Price"},{city:"Provo"}]);
var urlObj = url.parse(req.url, true, false);
//console.log("IN Getcity");
fs.readFile("cities.dat.txt", function (err, data) {
if (err) throw err;
var cities = data.toString().split("\n");
var myRe = new RegExp("^"+urlObj.query["q"]);
//console.log(myRe);
var jsonresult = [];
for (var i = 0; i < cities.length; i++) {
var result = cities[i].search(myRe);
if(result != -1) {
//console.log(cities[i]);
jsonresult.push({city:cities[i]});
}
}
//console.log(jsonresult);
//console.log(JSON.stringify(jsonresult));
res.writeHead(200);
res.end(JSON.stringify(jsonresult));
});
});
app.get('/comment', function (req, res) {
//console.log("In comment route");
//resarray = [ { Name: 'Mickey', Comment: 'Hello',
//_id: '54f53d5ebf89e6100c2180da' },
//{ Name: 'Mark', Comment: 'This is a comment',
//_id: '54f53e21801a52330c04be8a' }
//]
//res.json(resarray);
//console.log("In GET");
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost/weather", function(err, db) {
if(err) throw err;
db.collection("comments", function(err, comments){
if(err) throw err;
comments.find(function(err, items){
items.toArray(function(err, itemArr){
//console.log("Document Array: ");
//console.log(itemArr);
//res.writeHead(200);
//res.end(JSON.stringify(itemArr));
res.json(itemArr);
});
});
});
});
});
app.post('/comment', auth, function (req, res) {
//console.log("In POST comment route");
//console.log(req.user);
//console.log(req.body);
//console.log(req.body.Name);
//console.log(req.body.Comment);
//console.log("Remote User");
//console.log(req.remoteUser);
//console.log("POST comment route");
//var jsonData = "";
//req.on('data', function (chunk) { jsonData += chunk; });
//req.on('end', function () {
//var reqObj = JSON.parse(jsonData);
//console.log(reqObj);
//console.log("Name: "+reqObj.Name);
//console.log("Comment: "+reqObj.Comment);
var reqObj = req.body;
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost/weather", function(err, db) {
if(err) throw err;
db.collection('comments').insert(reqObj,function(err, records) {
//console.log("Record added as "+records[0]._id);
//res.writeHead(200);
//res.end("");
res.status(200);
res.end();
});
});
//});
//res.status(200);
//res.end();
//res.status(200);
//res.end();
});