diff --git a/zip-api/app.js b/zip-api/app.js index df1f97b..230c4e9 100644 --- a/zip-api/app.js +++ b/zip-api/app.js @@ -6,24 +6,53 @@ const zipdb = require('./zipData'); const PORT = process.env.PORT || 8000; -// console.log(zipdb.byCity); app.get('/', (req, res) => { - res.json({test: 'Yay'}); + res.json({ test: 'Yay' }); }); -app.get('/zip/:zipcode', (req, res) => { - // fill in... -}); + + app.get('/city/:cityname', (req, res) => { - // fill in... + const cities = req.params.cityname.toUpperCase(); + + + if (/^[a-zA-Z]+$/.test(cities)) { + + const results = zipdb.byCity[cities]; + if (results === undefined) { + res.json("city not found!"); + } + res.json(results); + } else { + res.json("city not found!"); + } }); app.listen(PORT, () => { - console.log(`zip-api is up and running on ${PORT}`); + console.log(`zip-api is up and running on http://localhost:${PORT}`); }); + +app.get('/zip/:zipcode', (req, res) => { + const zipcode = req.params.zipcode; + + + if (zipcode.length === 5) { + + const results = zipdb.byZip[zipcode]; + if (results === undefined) { + res.json("zip not found!"); + } + res.json(results); + } else { + res.json("zip not found!"); + } + + + +}); \ No newline at end of file diff --git a/zip-api/package.json b/zip-api/package.json index 631bb53..91868e2 100644 --- a/zip-api/package.json +++ b/zip-api/package.json @@ -16,4 +16,4 @@ "devDependencies": { "nodemon": "^1.19.4" } -} +} \ No newline at end of file