Skip to content

Instantly share code, notes, and snippets.

@jinwook-k
Last active September 16, 2020 14:55
Show Gist options
  • Save jinwook-k/810623510e54dce59d8f400672b8112b to your computer and use it in GitHub Desktop.
Save jinwook-k/810623510e54dce59d8f400672b8112b to your computer and use it in GitHub Desktop.
server/api/index.js
...
// POST Request - dynamically get the weather data based on request body
router.post("/weather", async (req, res) => {
...
});
// POST Request - get the weather data from the api, save it to mongo, then return the data back
router.post("/weatherMongo", async(req, res) => {
const {zipCode, tempMetric} = req.body;
let weather = new Weather();
let weatherData = await weather.getWeatherData(zipCode, tempMetric);
await weather.saveWeatherDataToMongo(zipCode, weatherData);
res.header("Content-Type",'application/json');
res.send(JSON.stringify(weatherData, null, 4));
})
// GET Request - get the weather data saved from Mongo
router.get("/weatherMongo", async(req, res) => {
const {zipCode} = req.query;
let weather = new Weather();
let weatherData = await weather.getWeatherDataFromMongo(zipCode);
res.header("Content-Type",'application/json');
res.send(JSON.stringify(weatherData, null, 4));
})
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment