diff --git a/index.js b/index.js index a7cfaa56a..cb1a1b55c 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,30 @@ const express = require("express"); +const fs = require("fs"); +const path = require("path"); const app = express(); -app.get("/", (req, res) => { - res.send("Express on Vercel"); + +// Middleware to handle JSON data +app.use(express.json()); + +// API endpoint to get JSON data +app.get("/api/desa/:kodedesa/:tahun", (req, res) => { + const { kodedesa, tahun } = req.params; + + // Define the path to the JSON file + const filePath = path.join(__dirname, "data", kodedesa, tahun, "file.json"); + + // Read the JSON file + fs.readFile(filePath, "utf8", (err, data) => { + if (err) { + // Handle file not found or other errors + return res.status(404).json({ message: "File not found" }); + } + + // Send the JSON data as the response + res.json(JSON.parse(data)); + }); }); + const PORT = process.env.PORT || 5000; app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`);