Skip to content

Instantly share code, notes, and snippets.

@vvasc
Last active November 10, 2023 20:08
Show Gist options
  • Save vvasc/cb27521ee1647e1b1bd7eed036aeb5f4 to your computer and use it in GitHub Desktop.
Save vvasc/cb27521ee1647e1b1bd7eed036aeb5f4 to your computer and use it in GitHub Desktop.
Live Coding
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)) );

Live Coding Test: Express.js Integration Challenge

Objective:

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.

Starting Point:

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

API Integration:

You will integrate two external endpoints:

Tasks

  1. Retrieve and combine data from both APIs.
  2. Structure the final output so that each album includes all its related photos. The structure should be intuitive and easy to navigate. Endpoint Creation:
  3. 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.

Expectations:

  • 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.
@aronis-clearco
Copy link

/// 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";

async initialize(){
    try{
        const { data: albumData } = await axios.get(this.AlbumDataUrl);
        this.AlbumData = albumData;
        const { data: albumDataPhoto } = await axios.get(this.AlbumDataPhotosUrl);
        this.AlbumDataPhoto = albumDataPhoto;
    } catch (error){
        console.log("Error initializing", error);
        throw error;
    }
}

async getAlbum(id){
    if(!this.AlbumData.length){
        await this.initialize();
    }
    const album = this.AlbumData.find(entry => entry.id===id );

    if(!album){
        return {eror: true, status: 404, message: "Album not found"};
    }

    album.photos = this.AlbumDataPhoto.filter(entry => entry.albumId === album.id);
    return album;
}

}

module.exports= {
Album
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment