Skip to content

Commit

Permalink
start on handling postcode and coords requests > relates #17 #18
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtualDOMinic committed Aug 2, 2018
1 parent 2d6a174 commit 0cf649a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
46 changes: 38 additions & 8 deletions src/handlers.js
Original file line number Diff line number Diff line change
@@ -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);
};
Expand Down Expand Up @@ -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
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 0cf649a

Please sign in to comment.