diff --git a/README.md b/README.md index c1b376b..2a53cc5 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,55 @@ -# Zip Code API Lab +# Zip Code API 🔥 -## Project 00: Hello World in Express.js +## the Zip API service -Follow instructions in class to create an npm project from scratch, and make a simple hello world web app using Express.js. +The Zip Code API , when called, returns a json object with information regarding the specified zipcode or city. -> Note: you do not have to hand this in. +![Demo](https://i.imgur.com/R9qOpqR.gif) -## Project 01: Build the Zip API service -This week we will build the Zip code API that we used last week in-class. +Example of how to call the API +``` +http://ctp-zip-api.herokuapp.com/zip/42069 +``` +or if you want to run locally after cloning this repo -> Note: The documentation for the API can be found here: https://github.com/CUNYTechPrep/zip-code-react-lab#the-zip-and-city-search-api-documentation-ctp-zip-api +``` +http://localhost:8000/zip/42069 +``` +Output : +``` +[{ +"RecordNumber":"23208", +"Zipcode":"42069", +"ZipCodeType":"STANDARD", +"City":"MELBER", +"State":"KY", +"LocationType":"PRIMARY", +"Lat":"36.91", +"Long":"-88.75", +"Xaxis":"0.01", +"Yaxis":"-0.79", +"Zaxis":"0.60", +"WorldRegion":"NA", +"Country":"US", +"LocationText":"Melber, KY", +"Location":"NA-US-KY-MELBER", +"Decommisioned":"false", +"TaxReturnsFiled":"421", +"EstimatedPopulation":"807", +"TotalWages":"10888387", +"Notes":"" +}] +``` + + +> Note: http://ctp-zip-api.herokuapp.com/ is the URL for the API to be used in your app. + +> To run the program on your local device, clone this repo and : - `cd zip-api` - `npm install` -- Add the code for the `/zip` and `/city` routes +- read the code for the `/zip` and `/city` routes to understand how the API calls and responses are being made - `npm start` to start dev server - Visit: + http://localhost:8000/ diff --git a/zip-api/app.js b/zip-api/app.js index df1f97b..bf88de9 100644 --- a/zip-api/app.js +++ b/zip-api/app.js @@ -15,12 +15,26 @@ app.get('/', (req, res) => { app.get('/zip/:zipcode', (req, res) => { - // fill in... + let zip = req.params.zipcode; + let response = zipdb.byZip[zip]; + + if(response === undefined){ + res.status(404).send('ERROR 404, ZIPCODE NOT FOUND'); + }else{ + res.json(response); + } }); app.get('/city/:cityname', (req, res) => { - // fill in... + let city = req.params.cityname; + let response = zipdb.byCity[city]; + if(response === undefined){ + res.status(404).send('ERROR 404, CITY NOT FOUND'); + }else{ + res.json(response); + } + }); diff --git a/zip-api/package.json b/zip-api/package.json index 631bb53..32e33ca 100644 --- a/zip-api/package.json +++ b/zip-api/package.json @@ -4,9 +4,9 @@ "description": "Simple API backend for CTP lecture.", "main": "app.js", "scripts": { - "start": "node app.js" + "start": "nodemon app.js" }, - "author": "Edgardo Molina", + "author": "Mohammed Chowdhurys", "license": "ISC", "dependencies": { "csv-parse": "^4.12.0",