Skip to content
Permalink
Browse files

Updated layout and added subscribe validation

  • Loading branch information...
james-tyner committed Mar 4, 2019
1 parent 30c4b7f commit ce04d656c95ada65698788fc8bc40f3fd18e7f29
@@ -8,3 +8,4 @@ permit_data.db
email.db
futureIndex.html
encryption/*
runSendEmail*
216 app.js
@@ -5,6 +5,10 @@ let express = require("express");
let app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded());
var exphbs = require('express-handlebars');

const { check, validationResult } = require('express-validator/check');
const { sanitizeBody } = require('express-validator/filter');

var fs = require('fs');
var key = fs.readFileSync('encryption/private.key');
@@ -13,8 +17,20 @@ var https = require('https');

require('dotenv').load();

app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

app.get('/', function (req, res) {
res.render('home', {layout: false});
});

app.use(express.static("public"));

axios.interceptors.request.use(request => {
console.log('Starting Request', request)
return request
});


// Returns array of ZIP objects containing neighborhood descriptions
app.get('/api/neighborhoods', function(request, response){
@@ -33,7 +49,7 @@ app.get('/api/neighborhoods', function(request, response){
zipHolder = [];
rows.forEach((row) => {
zipHolder.push({
"zip-code":row.ZIP,
"zipCode":row.ZIP,
"description":row.Description
});
});
@@ -68,33 +84,33 @@ app.get('/api/neighborhoods/:zip', function(request, response){
"zip-code":row.ZIP,
"description":row.Description,
"units":{
"units-all":row.UnitsAll,
"units-2019":row.Units2019,
"units-2018":row.Units2018,
"units-2017":row.Units2017,
"units-2016":row.Units2016,
"units-2015":row.Units2015,
"units-2014":row.Units2014,
"units-2013":row.Units2013
"unitsAll":row.UnitsAll,
"units2019":row.Units2019,
"units2018":row.Units2018,
"units2017":row.Units2017,
"units2016":row.Units2016,
"units2015":row.Units2015,
"units2014":row.Units2014,
"units2013":row.Units2013
},
"demographics":{
"population":row.Population,
"white":row.White,
"white-non-hispanic":row.White_Non_Hispanic,
"whiteNonHispanic":row.White_Non_Hispanic,
"black":row.Black,
"native-american":row.Native_American,
"nativeAmerican":row.Native_American,
"asian":row.Asian,
"hawaiian-pacific":row.Hawaiian_Pacific,
"hawaiianPacific":row.Hawaiian_Pacific,
"other":row.Other,
"two_or_more":row.Two_Or_More,
"two_or_more_other":row.Two_Or_More_Other,
"two_or_more_not_other":row.Two_Or_More_Not_Other,
"percent-white":row.Percent_White_Non_Hispanic,
"percent-minority":row.Percent_Minority
"twoOrMore":row.Two_Or_More,
"twoOrMoreOther":row.Two_Or_More_Other,
"twoOrMoreNotOther":row.Two_Or_More_Not_Other,
"percentWhite":row.Percent_White_Non_Hispanic,
"percentMinority":row.Percent_Minority
},
"costs":{
"median-income":row.MedIncome,
"median-housing-cost":row.MedHousingCost,
"medianIncome":row.MedIncome,
"medianHousingCost":row.MedHousingCost,
"percentage":row.Percentage
}
});
@@ -113,26 +129,154 @@ app.get('/api/neighborhoods/:zip', function(request, response){
// Return all projects for one ZIP code
// Here you set it to return 50 at a time. The offset parameter will tell the Socrata API which 50 to pull. In your page, use ?page=NUMBER to return the proper page, or leave it blank to get the most recent 50
app.get('/api/projects/:zip', function(req, res){
axios.get('/resource/75vw-v4fk.json', {
baseURL: 'https://data.lacity.org',
headers: {"X-App-Token": process.env.SOCRATA_API_KEY},
params: {
$limit:50,
$offset:(req.query.page || 0),
$order:"issue_date DESC",
$where:`permit_type = 'Bldg-New' AND permit_sub_type not in ('Commercial') AND zip_code = ${req.params.zip}`
}
})
.then(function (response) {
res.send(response.data);
})
.catch(function (error) {
console.log(error);
});
axios.get('/resource/75vw-v4fk.json', {
baseURL: 'https://data.lacity.org',
headers: {"X-App-Token": process.env.SOCRATA_API_KEY},
params: {
$limit:50,
$offset:(req.query.page || 0),
$order:"issue_date DESC",
$where:`permit_type = 'Bldg-New' AND permit_sub_type not in ('Commercial') AND zip_code = ${req.params.zip} AND ${lastWeek}`
}
})
.then(function (response) {
res.send(response.data);
})
.catch(function (error) {
console.log(error);
});
});


app.post('/email/subscribe', function(request, response){
app.post('/email/subscribe', [
check('email').isEmail().normalizeEmail().withMessage("That’s not a valid email address."),
check('zip').isLength(5).isIn([
90042,
90028,
90062,
91601,
90044,
91316,
90036,
90011,
90048,
91605,
91602,
90045,
90037,
91401,
91607,
90066,
90230,
91406,
91325,
91436,
90025,
90049,
90065,
90291,
91403,
90002,
90038,
91326,
91345,
90003,
90068,
91324,
90272,
91307,
90069,
91402,
91311,
91606,
91356,
90034,
91604,
90026,
90061,
91306,
90043,
91304,
90041,
90077,
91352,
91331,
91367,
90004,
91423,
90024,
90035,
91342,
91411,
91344,
90064,
90094,
90744,
91343,
91405,
91040,
90063,
90032,
90006,
90020,
91335,
91042,
90501,
90019,
90046,
90029,
90031,
90059,
90731,
90210,
90067,
90023,
90039,
90016,
90001,
91364,
91303,
90007,
90047,
90293,
90732,
90027,
90015,
90710,
90018,
90012,
90033,
90005,
91504,
90402,
90017,
91205,
90008,
90248,
90292,
90014,
90247,
91105,
91340,
90057,
91214,
90013,
90717,
90010,
90232,
90212,
91505
]).trim().withMessage("That ZIP code isn’t listed in the city of Los Angeles.")
], function(request, response){
const errors = validationResult(request);
if (!errors.isEmpty()) {
// return response.status(422).json({ errors: errors.array() });
return response.status(422).render('home', {
layout: false,
errors: errors.array()
});
}

console.log(request.body.email);
console.log(request.body.zip);

@@ -0,0 +1,115 @@
90042,256,55596
90028,2213,40068
90062,104,40018
91601,2244,50832
90044,574,32278
91316,204,68720
90036,885,74279
90011,568,33824
90048,750,85396
91605,393,43004
91602,629,78526
90045,1886,90399
90037,548,31045
91401,465,51784
91607,453,60772
90066,606,75209
90230,128,79242
91406,429,51564
91325,164,65129
91436,196,159965
90025,1380,78713
90049,737,121671
90065,420,58405
90291,310,88078
91403,307,89552
90002,395,34000
90038,1290,36996
91326,731,105611
91345,37,74193
90003,826,34397
90068,194,82718
91324,555,68477
90272,514,180962
91307,24,111043
90069,288,86403
91402,462,41669
91311,553,82738
91606,361,44773
91356,170,71654
90034,612,65572
91604,546,99745
90026,1413,54342
90061,187,37126
91306,264,64772
90043,293,41812
91304,249,64367
90041,189,73250
90077,132,184722
91352,417,52171
91331,232,57353
91367,1419,84937
90004,1316,46581
91423,361,82554
90024,430,59418
90035,822,76677
91342,775,68600
91411,688,50332
91344,35,86460
90064,523,92194
90094,2661,136250
90744,471,43284
91343,412,61692
91405,1261,41283
91040,82,77910
90063,17,44121
90032,127,47370
90006,1198,33790
90020,661,42407
91335,682,55580
91042,282,53412
90501,419,64995
90019,517,50671
90046,676,65990
90029,456,37379
90031,311,41126
90059,148,37653
90731,271,49464
90210,104,149732
90067,293,123889
90023,213,40225
90039,406,81394
90016,258,43443
90001,217,35660
91364,410,100323
91303,731,51486
90007,530,23070
90047,57,42551
90293,20,100042
90732,11,89440
90027,316,58344
90015,4558,32979
90710,68,57968
90018,136,37341
90012,1398,38786
90033,317,31683
90005,923,32461
91504,0,73501
90402,18,140179
90017,645,28638
91205,2,38730
90008,110,36641
90248,166,58703
90292,395,103948
90014,2299,37619
90247,101,46076
91105,1,112765
91340,17,54703
90057,564,31337
91214,1,94322
90013,632,22808
90717,0,61077
90010,257,47115
90232,261,82254
90212,1,103032
91505,0,82630

0 comments on commit ce04d65

Please sign in to comment.
You can’t perform that action at this time.