const dbQuery = (query) => {
return new Promise( (resolve, reject) => {
setTimeout( ()=> { resolve(`Results for db query: ${query}`) }, 1000 * Math.floor(5*Math.random()) );
} )
}
const queries = ['Query 1', 'Query 2', 'Query 3', 'Query 4', 'Query 5'];
// Fix this snippet of code so that it prints the query results
queries.forEach( q => console.log(dbQuery(q)) );
Last active
November 10, 2023 20:08
-
-
Save vvasc/cb27521ee1647e1b1bd7eed036aeb5f4 to your computer and use it in GitHub Desktop.
Live Coding
Your task is to demonstrate your skills in backend development using Express.js by integrating two external APIs and creating new endpoints with specific functionalities.
Access the provided sandbox link containing a simple Express.js server setup: Express API Sandbox. Integration Task: https://codesandbox.io/p/sandbox/simple-express-server-forked-6sjsjq?file=%2Fsrc%2Findex.js%3A21%2C30
You will integrate two external endpoints:
-
Albums API: https://jsonplaceholder.typicode.com/albums Contains: id, title
-
Photos API: https://jsonplaceholder.typicode.com/photos Contains: url, title, albumId
- Retrieve and combine data from both APIs.
- Structure the final output so that each album includes all its related photos. The structure should be intuitive and easy to navigate. Endpoint Creation:
- Develop a new endpoint within the Express.js application. This endpoint should accept an id as a request parameter. It will filter and return the album corresponding to the passed id, along with its related photos.
- Code Quality: Write clean, efficient, and well-documented code.
- Error Handling: Implement proper error handling for external API calls and data processing.
- Correctness of the integration and data merging.
- Efficiency of the solution.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/// index.js
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const { Album } = require('./albumService');
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
//create a server object:
app.get("/", function(req, res) {
res.write("Hello World!..."); //write a response to the client
res.end(); //end the response
});
app.post("/", function(req, res) {
console.log(req.body);
res.send("received");
});
app.get("/:albumId", async function(req, res){
const album = new Album();
const result = await album.getAlbum(Number(req.params.albumId));
if(result.error){
res.status(result.status).json(result).end();
}
else res.json(result).end();
})
app.listen(8080, function() {
console.log("server running on 8080");
}); //the server object listens on port 8080
// STATUS: this works when sending GET or POST request
module.exports = app;
/// AlbumService.js
const axios = require('axios');
class Album {
AlbumData = [];
AlbumDataPhoto = [];
AlbumDataUrl = "https://jsonplaceholder.typicode.com/albums";
AlbumDataPhotosUrl = "https://jsonplaceholder.typicode.com/photos";
}
module.exports= {
Album
}