Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
andifahruddinakas committed Oct 24, 2024
1 parent 8bdf290 commit 9bc5147
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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}`);
Expand Down

0 comments on commit 9bc5147

Please sign in to comment.