-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
127 lines (88 loc) · 2.77 KB
/
server.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
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
var http = require('http');
var fs = require('fs');
var formidable = require("formidable");
var util = require('util');
var google = require('google');
var zipcodes = require('zipcodes');
var zcta = require("us-zcta-counties");
var express = require('express');
var bodyParser = require('body-parser');
var winston = require('winston');
var madison = require('madison');
require('winston-papertrail').Papertrail;
var winstonPapertrail = new winston.transports.Papertrail({
host: 'logs.papertrailapp.com',
port: 29959
})
winstonPapertrail.on('error', function(err) {
// Handle, report, or silently ignore connection errors and failures
});
var logger = new winston.Logger({
transports: [winstonPapertrail]
});
var app = express();
app.use(express.static('public'))
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post("/", function (req, res) {
var data = req.body;
findCountyAndState(data, res)
});
function findCountyAndState(data, res) {
var zipcode = data.zipcode;
console.log("zipcode entered is: " + zipcode);
var isValidZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/.test(zipcode);
console.log("Is this a valid zip? " + isValidZip);
if(isValidZip) {
zipcode = zipcode.substr(0,5);
console.log("processed zipcode is: " + zipcode);
}
console.log(zcta.find({zip: zipcode}));
if(zipcodes.lookup(zipcode)){
var dataType = data.dataType;
var city = zipcodes.lookup(zipcode).city;
var state = zipcodes.lookup(zipcode).state;
if(zcta.find({zip: zipcode})){
var county = zcta.find({zip: zipcode}).county;
} else {
var county = city;
}
var fullstate = madison.getStateNameSync(state);
if(state == "OR")
{ state = "Oregon"; }
console.log("zipcode:" + zipcode);
console.log("city: " + city);
console.log("state: " + state);
console.log("county: " + county);
if(dataType == "city") {
var searchPhrase = city + " " + state + " " +
"(City | Village | Township) Council Calendar " + fullstate + " Agenda";
}
if(dataType == "county") {
var searchPhrase = county + " " + state + " " +
"Council Calendar " + fullstate + " Agenda";
}
console.log(searchPhrase);
searchFor(searchPhrase, res);
}
else
{
var searchPhrase = data.zipcode + " " +
"Meetings Calendar Agenda";
console.log(searchPhrase);
searchFor(searchPhrase, res);
}
}
function searchFor(searchPhrase, res) {
google(searchPhrase, function (err, gRes){
if (err) logger.info(err)
var url = gRes.links[0].href;
console.log("url: " + url);
res.redirect(url);
res.end();
});
}
app.listen(1185);
console.log("server listening on 1185");