Skip to content

Instantly share code, notes, and snippets.

@masiamj
Last active December 22, 2020 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masiamj/6c811461d32110ba8f6e2bc39124e3c0 to your computer and use it in GitHub Desktop.
Save masiamj/6c811461d32110ba8f6e2bc39124e3c0 to your computer and use it in GitHub Desktop.
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