|
| 1 | +### This is a Node Express API |
| 2 | + |
| 3 | + |
| 4 | +I using an external json call to feed my examples |
| 5 | + |
| 6 | +``` |
| 7 | +npm install |
| 8 | +``` |
| 9 | + |
| 10 | +``` |
| 11 | +npm start |
| 12 | +``` |
| 13 | + |
| 14 | +I used axios package because request and request-promise with Q didn't load the external |
| 15 | +feed. |
| 16 | +router/index.js |
| 17 | +```JS |
| 18 | + |
| 19 | +var URL = 'http://ori-nodeassets.nbcnews.com/elections/2018-05-08/races_S_counties.json'; |
| 20 | + |
| 21 | +router.get('/races/:variable*?', function (req, res, next) { |
| 22 | + axios.get(URL).then(function (response) { |
| 23 | + res.json(dal.apiRaces(req.params.variable, response.data)); |
| 24 | + }); |
| 25 | + |
| 26 | +}); |
| 27 | +router.get('/candidates/:variable*?', function (req, res, next) { |
| 28 | + axios.get(URL).then(function (response) { |
| 29 | + res.json(dal.apiCandidates(req.params.variable, response.data)); |
| 30 | + }); |
| 31 | +}); |
| 32 | +router.get('/counties/:variable*?', function (req, res, next) { |
| 33 | + axios.get(URL).then(function (response) { |
| 34 | + res.json(dal.apiCounties(req.params.variable, response.data)); |
| 35 | + }); |
| 36 | +}); |
| 37 | +``` |
| 38 | +dataLayer.js example: |
| 39 | + |
| 40 | +```JS |
| 41 | + |
| 42 | +const apiRaces = (variable, body) => { |
| 43 | + var races = variable ? body.races.filter(x => x.race_name === variable) : body.races; |
| 44 | + return races.map( c => { |
| 45 | + return { |
| 46 | + race_id: c.race_id, |
| 47 | + election_date: c.election_date, |
| 48 | + party: c.party, |
| 49 | + state_name: c.state_name, |
| 50 | + total_vote: c.total_vote, |
| 51 | + percent_in: c.percent_in |
| 52 | + |
| 53 | + }; |
| 54 | + }); |
| 55 | +}; |
| 56 | +const apiCandidates = (variable, body) => { |
| 57 | + var races = variable ? body.races.map(x => { |
| 58 | + if (x.candidates.filter(y => y.candidate_code == parseInt(variable))){ |
| 59 | + return x; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + }) : body.races; |
| 64 | + return races.reduce((acc, val) => acc.concat(val.candidates.filter(y => variable ? y.candidate_code == parseInt(variable) : y.candidate_code).map(c => { |
| 65 | + return { |
| 66 | + |
| 67 | + candidate_code: c.candidate_code, |
| 68 | + major_party: c.major_party, |
| 69 | + race_name: val.race_name, |
| 70 | + vote: c.vote, |
| 71 | + electoral_vote: c.electoral_vote |
| 72 | + }; |
| 73 | + })), []); |
| 74 | +}; |
| 75 | +const apiCounties = (variable, body) => { |
| 76 | + var races = variable ? body.races.map(x => { |
| 77 | + if (x.counties.filter(y => y.county_name === variable)){ |
| 78 | + return x; |
| 79 | + } |
| 80 | + |
| 81 | + }) : body.races; |
| 82 | + |
| 83 | + console.log(races); |
| 84 | + return races.reduce((acc, val) => |
| 85 | + acc.concat(val.counties.filter(x => variable ? x.county_name === variable : x.county_name).map(c => |
| 86 | + |
| 87 | + |
| 88 | + { |
| 89 | + return { |
| 90 | + county_code: c.county_code, |
| 91 | + party_stratum_name: c.party_stratum_name, |
| 92 | + registered: c.registered, |
| 93 | + total_vote: c.total_vote, |
| 94 | + fips_code: c.fips_code, |
| 95 | + race_id: val.race_id |
| 96 | + }; |
| 97 | + })), []); |
| 98 | + |
| 99 | +}; |
| 100 | +module.exports = { |
| 101 | + // export methods here |
| 102 | + apiRaces: apiRaces, |
| 103 | + apiCandidates: apiCandidates, |
| 104 | + apiCounties: apiCounties |
| 105 | +}; |
| 106 | +``` |
| 107 | +api call screenshots: |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
0 commit comments