From 58e60409b36621ad559b8b4c34df8e5bca65b059 Mon Sep 17 00:00:00 2001 From: Andi Fahruddin Akas Date: Thu, 24 Oct 2024 18:54:31 +0800 Subject: [PATCH] Update index.js --- index.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) 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,