Last active
December 22, 2020 17:52
-
-
Save masiamj/6c811461d32110ba8f6e2bc39124e3c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const get = require('lodash.get') | |
const keyBy = require('lodash.keyBy') | |
/** | |
* Initializes express application | |
* You should include whatever middleware you need here (cors, etc.) | |
*/ | |
const app = express() | |
/** | |
* Query whatever DB you have to get a mapping of ID --> Logo URL | |
*/ | |
const logos = await db.query(`select id, logoUrl from companies`) | |
/** | |
* Convert the logos list to an object for O(1) lookups | |
*/ | |
const logosKeyedByID = keyBy(logos, 'id') | |
/** | |
* Include one endpoint with a single parameter (the ID) the lookup a logo URL | |
*/ | |
app.get('/:id', (req, res, next) => { | |
const id = get(req, ['query', 'id']) | |
/** | |
* Handle bad request | |
*/ | |
if (!id) { | |
return next('No ID provided') | |
} | |
/** | |
* Lookup the Logo with some default URL | |
*/ | |
const logo = get(logosKeyedById, [id, 'logoUrl'], 'some default logo URL') | |
/** | |
* Send back the logo URL | |
*/ | |
res.status(200).send(logo) | |
}) | |
/** | |
* Listen | |
*/ | |
app.listen(process.env.PORT, () => { | |
console.log(`Server listening on ${process.env.PORT}`) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment