diff --git a/index.js b/index.js index 50d857a6b..053bcf909 100644 --- a/index.js +++ b/index.js @@ -29,18 +29,18 @@ app.get("/api/desa", (req, res) => { app.get("/api/desa/:kodedesa/:tahun", (req, res) => { const { kodedesa, tahun } = req.params; - const filePath = path.join( - __dirname, - "public", - "desa", - kodedesa, - tahun + ".json" - ); + const folderPath = path.join(__dirname, "public", "desa", kodedesa); + const filePath = path.join(folderPath, tahun + ".json"); + + if (!fs.existsSync(folderPath)) { + return res.status(400).json({ + status: 400, + error: true, + message: `kodedesa ${kodedesa} tidak dikenali`, + }); + } - if ( - !fs.existsSync(path.join(__dirname, "public", "desa", kodedesa)) || - !fs.existsSync(filePath) - ) { + if (!fs.existsSync(filePath)) { return res.status(400).json({ status: 400, error: true,