Skip to content

Instantly share code, notes, and snippets.

@dennbagas
Last active September 17, 2019 12:12
Show Gist options
  • Save dennbagas/29be1e9a5da153d434c80746b0c0f43f to your computer and use it in GitHub Desktop.
Save dennbagas/29be1e9a5da153d434c80746b0c0f43f to your computer and use it in GitHub Desktop.
const fs = require("fs");
module.exports.index = async (req, res, next) => {
const rawcity = req.query.kota;
let city;
if (rawcity) {
city = rawcity.toLowerCase()
.split(' ')
.map((s) => s.charAt(0).toUpperCase() + s.substring(1))
.join(' ');
} else {
res.send("parameter kota tidak boleh kosong");
}
fs.readFile('cache/weather.json', (err, data) => {
let weather = JSON.parse(data.toString());
let weatherData = weather.filter((element) => element.kota.includes(city));
if (weatherData.length > 0) {
res.send(weatherData[0]);
} else {
res.send("kota tidak ditemukan");
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment