Skip to content

Instantly share code, notes, and snippets.

@jinwook-k
Last active July 28, 2020 06:07
Show Gist options
  • Save jinwook-k/a92cc4cf6be7c703897041b1f1a98b20 to your computer and use it in GitHub Desktop.
Save jinwook-k/a92cc4cf6be7c703897041b1f1a98b20 to your computer and use it in GitHub Desktop.
server/api/index.js
// Use express's router to route all our API endpoints
const express = require('express');
const router = express.Router();
// Use the weather class we made in ./weather.js to call our method that will get the weather data from the api
const Weather = require("./weather");
// GET Request - statically get the weather data from the weather api
router.get("/weather", async (req, res) => {
let weather = new Weather();
// Fixing the params of zipcode and tempMetric for an example GET request
let weatherData = await weather.getWeatherData(98052, "us");
// Content that will be sent will be a prettified json
res.header("Content-Type",'application/json');
res.send(JSON.stringify(weatherData, null, 4));
});
// POST Request - dynamically get the weather data based on request body
router.post("/weather", async (req, res) => {
const {zipCode, tempMetric} = req.body;
let weather = new Weather();
// The params for zipCode and tempMetric are dynamic
let weatherData = await weather.getWeatherData(zipCode, tempMetric);
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