diff --git a/src/handlers.js b/src/handlers.js index c598e9a..aeb069a 100644 --- a/src/handlers.js +++ b/src/handlers.js @@ -1,7 +1,7 @@ const fs = require("fs"); const path = require("path"); const mime = require("mime-types"); -const request = require("request"); +const requestPkg = require("request"); const buildPath = function(myPath) { return path.join(__dirname, "..", "public", myPath); }; @@ -29,17 +29,47 @@ const handler = { console.log(error); return; } - response.writeHead(200, {"Content-Type": mime.lookup(request.url)}); + response.writeHead(200, { "Content-Type": mime.lookup(request.url) }); response.end(file); }); }, coordsHandler: function(request, response) { - // PLACEHOLDER - const latlong = request.url.split("coords/")[1] - const lat = latlong.split("&long=")[0].split("=")[1];) - const long = latlong.split("&long=")[1]; - response.writeHead(302, {"Content-Type": "text/plain"}); - response.end(); + const latlong = request.url.split("coords/")[1]; + const lat = latlong.split("&long=")[0].split("=")[1]; + const long = latlong.split("&long=")[1]; + console.log(`lat ${lat} long ${long}`) + response.writeHead(302, { "Content-Type": "text/plain" }); + response.end(); + + // NEXT STEPS: + // redirect user to home url, + // send request (using lat and long) to police API, + // get results from police + // send object of some sorts to client + + }, + postcodeHandler: function(request, response) { + const decoded = decodeURI(request.url); + const postcode = decoded.split("postcode/")[1].replace(/[^A-Za-z0-9]/, ""); + + // API request to get latitude and longitude from postcodes.io api + requestPkg(`https://api.postcodes.io/postcodes/${postcode}`, function(error, response, body){ + console.log(`error: ${error}`); + const statusCode = response.statusCode + body = JSON.parse(body); + console.log(`lat: ${body.result.latitude}, long: ${body.result.longitude}`); + }) + + // PLACEHOLDER + console.log(`postcode is ${postcode}`); + response.writeHead(302, { "Content-Type": "text/plain" }); + response.end(); + + // NEXT STEPS: + // redirect user to home url, + // send request (using lat and long) to police API, + // get results from police + // send object of some sorts to client } }; diff --git a/src/routes.js b/src/routes.js index 60bb7b6..7a0e759 100644 --- a/src/routes.js +++ b/src/routes.js @@ -7,6 +7,8 @@ const router = (req, res) => { handler.publicHandler(req, res); } else if (req.url.includes("/coords/")) { handler.coordsHandler(req, res); + } else if (req.url.includes("/postcode/")) { + handler.postcodeHandler(req, res); } else { // handler.publicHandler(req, res); res.writeHead(404, "Content-Type: text/html")