Created
August 10, 2018 15:59
-
-
Save SuperManEver/d784d30a71702ab974f63b4158652d7a 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 fs = require('fs'); | |
const cors = require('cors'); | |
const _ = require('lodash'); | |
const PORT = process.env.PORT || 5000; | |
const buffer = fs.readFileSync('./data.json'); | |
const { flights } = JSON.parse(buffer); | |
const carriers = _.uniq(flights.map(flight => flight.carrier)); | |
const app = express(); | |
app.use(cors()); | |
app.get('/', (req, res) => res.send('Hello World!')); | |
app.get('/carriers', (req, res) => { | |
res.json(carriers); | |
}); | |
app.get('/flights', (req, res) => { | |
if (req.query.hasOwnProperty('carriers')) { | |
if (req.query['carriers'].length === 0) { | |
res.json(flights); | |
return; | |
} | |
const providedCarriers = req.query['carriers'] | |
.split(',') | |
.map(str => _.trim(str)); | |
const result = flights.filter(flight => | |
providedCarriers.includes(flight.carrier), | |
); | |
res.json(result); | |
} else { | |
res.json(flights); | |
} | |
}); | |
app.listen(PORT, () => console.log(`api server listening on port ${PORT}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment