diff --git a/zip-api/app.js b/zip-api/app.js index df1f97b..4945928 100644 --- a/zip-api/app.js +++ b/zip-api/app.js @@ -10,20 +10,47 @@ const PORT = process.env.PORT || 8000; app.get('/', (req, res) => { - res.json({test: 'Yay'}); + res.json({ test: `Welcome to Jessica's ZIP API lab - to get a zip code add zip/[zipcode] or city/[cityname] to the existing url` }); }); app.get('/zip/:zipcode', (req, res) => { - // fill in... + const zipcode = req.params.zipcode; + + + + if (zipcode.length === 5 && (zipcode.match(/^[0-9]+$/) != null)) { + const results = zipdb.byZip[zipcode]; + res.json(results); + } + else { + res.status(404).send("Not Found"); + } }); app.get('/city/:cityname', (req, res) => { - // fill in... + const cityname = req.params.cityname.toUpperCase(); + // console.log(cityname); + + //const tester = encodeURIComponent(cityname); + //console.log(tester); + + if (cityname.length >= 3) { + const results = zipdb.byCity[cityname]; + //console.log(results); + if (results === undefined) { + res.status(404).send("Not Found"); + } + res.json(results); + } + + else { + res.status(404).send("Not Found"); + } }); app.listen(PORT, () => { console.log(`zip-api is up and running on ${PORT}`); -}); +}); \ No newline at end of file