I had an interview task which was the final stage, however, the feedback I got back for my solution was that it lacked effort and structure
. I would be grateful to know how my solution could be improved. I used postcode.io to get the latitude and longitude from the post codes.
Here is the Description:
Brief: Create a server application that allows a user to input one or more postcodes, and then returns location-specific information based on the postcode entered.
Requirements: At least one of the external APIs made available to you should be used to retrieve the information that your API returns. You are not limited to using only one, and are free to use as many of them as you like (inc. any other external resources) in order to provide useful data.
Based on the above I made the following program.
app.js
import * as express from "express";
import * as axios from "axios";
const app = express();
app.use(express.json());
app.get("/", (req, res) => {
res.send("Server is up and running 🤓");
});
app.get("/postcode/:postcode", async (req, res) => {
const postcode = req.params.postcode;
await getLatlonFromPostcode(postcode).then((data) => {
res.send(data);
});
});
app.post("/postcodes", async (req, res) => {
const postCodes = req.body.postcodes;
const result = await getAdressForMultiplePostCodes(postCodes).then(
(response) => {
return returnLatLonObj(response.result);
}
);
res.json(result);
});
//Return the lat and lon for a single postcode
export async function getLatlonFromPostcode(postcode) {
try {
const res = await axios
.get(`https://api.postcodes.io/postcodes/${postcode}`)
.then((response) => {
const lat = response.data.result.latitude.toString();
const lon = response.data.result.longitude.toString();
return `${lat}, ${lon}`;
});
return res;
} catch (error) {
console.error(error);
}
}
//Return array of addresses from Postcode array
export async function getAdressForMultiplePostCodes(postcodes) {
try {
const res = await axios
.post("https://api.postcodes.io/postcodes", {
postcodes,
})
.then((response) => {
return response.data;
});
return res;
} catch (error) {
console.error(error);
}
}
//Grab Latitude and Longitude and convert Array to object
function returnLatLonObj(arr) {
return arr.reduce((prev, curr) => {
prev[curr.result.latitude] = curr.result.longitude;
return prev;
}, {});
}
export default app;
This is my index.js
import app from "./app";
app.listen(3000, () => {
console.log("server listening on port 3000");
})
This was my test file, which I used jest for.
import { getLatlonFromPostcode } from "../app";
describe("getLatlonFromPostcode", () => {
test("it should return latlon for given postcode", async () => {
const latlon = await getLatlonFromPostcode("ox495nu");
expect(latlon).toEqual("51.655929, -1.069752");
});
});
describe("getLatlonFromPostcode in uppercase", () => {
test("it should return latlon for given postcode", async () => {
const latlon = await getLatlonFromPostcode("OX49 5NU");
expect(latlon).toEqual("51.655929, -1.069752");
});
});
describe("getLatlonFromPostcode with spaces between the postcode", () => {
test("it should return latlon for given postcode", async () => {
const latlon = await getLatlonFromPostcode("OX49 5NU");
expect(latlon).toEqual("51.655929, -1.069752");
});
});