CoolFace
Apppublic

devusman/indonesian

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
index.js72 linesDownload Raw Back to root
1const express = require('express');2const path = require('path');3const fs = require('fs').promises;4 5const app = express();6const port = 7860;7 8// Set EJS as the templating engine9app.set('view engine', 'ejs');10app.set('views', path.join(__dirname, 'views'));11 12// Serve the dist directory as a static folder13app.use('/api', express.static(path.join(__dirname, 'data')));14 15// Route to render the EJS testing page16app.get('/', async (req, res) => {17    try {18        const provincesData = await fs.readFile(path.join(__dirname, 'data', 'provinces.json'), 'utf8');19        const provinces = JSON.parse(provincesData);20        res.render('index', { provinces });21    } catch (error) {22        console.error(error);23        res.status(500).send('Error loading province data.');24    }25});26 27// API endpoint for provinces28app.get('/api/provinces', async (req, res) => {29    try {30        const data = await fs.readFile(path.join(__dirname, 'data', 'provinces.json'), 'utf8');31        res.json(JSON.parse(data));32    } catch (error) {33        res.status(404).json({ error: 'Provinces not found' });34    }35});36 37// API endpoint for regencies in a specific province38app.get('/api/provinces/:provinceId/regencies', async (req, res) => {39    const { provinceId } = req.params;40    try {41        const data = await fs.readFile(path.join(__dirname, 'data', provinceId, 'regencies.json'), 'utf8');42        res.json(JSON.parse(data));43    } catch (error) {44        res.status(404).json({ error: 'Regencies not found for this province' });45    }46});47 48// API endpoint for districts in a specific regency49app.get('/api/provinces/:provinceId/regencies/:regencyId/districts', async (req, res) => {50    const { provinceId, regencyId } = req.params;51    try {52        const data = await fs.readFile(path.join(__dirname, 'data', provinceId, regencyId, 'district.json'), 'utf8');53        res.json(JSON.parse(data));54    } catch (error) {55        res.status(404).json({ error: 'Districts not found for this regency' });56    }57});58 59// API endpoint for villages (subdistricts) in a specific district60app.get('/api/provinces/:provinceId/regencies/:regencyId/districts/:districtId/villages', async (req, res) => {61    const { provinceId, regencyId, districtId } = req.params;62    try {63        const data = await fs.readFile(path.join(__dirname, 'data', provinceId, regencyId, districtId, 'subdistrict.json'), 'utf8');64        res.json(JSON.parse(data));65    } catch (error) {66        res.status(404).json({ error: 'Villages not found for this district' });67    }68});69 70app.listen(port, () => {71    console.log(`Server is running on http://localhost:${port}`);72});