File size: 2,632 Bytes
b205898
 
 
 
 
a3a2ef5
b205898
 
 
 
 
 
3034be5
b205898
 
 
 
3034be5
b205898
 
 
 
 
 
 
 
 
 
 
3034be5
b205898
 
 
 
 
 
 
 
 
 
3034be5
b205898
 
 
 
 
 
 
 
 
 
3034be5
b205898
 
 
 
 
 
 
 
 
 
3034be5
b205898
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const express = require('express');
const path = require('path');
const fs = require('fs').promises;

const app = express();
const port = 7860;

// Set EJS as the templating engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

// Serve the dist directory as a static folder
app.use('/api', express.static(path.join(__dirname, 'data')));

// Route to render the EJS testing page
app.get('/', async (req, res) => {
    try {
        const provincesData = await fs.readFile(path.join(__dirname, 'data', 'provinces.json'), 'utf8');
        const provinces = JSON.parse(provincesData);
        res.render('index', { provinces });
    } catch (error) {
        console.error(error);
        res.status(500).send('Error loading province data.');
    }
});

// API endpoint for provinces
app.get('/api/provinces', async (req, res) => {
    try {
        const data = await fs.readFile(path.join(__dirname, 'data', 'provinces.json'), 'utf8');
        res.json(JSON.parse(data));
    } catch (error) {
        res.status(404).json({ error: 'Provinces not found' });
    }
});

// API endpoint for regencies in a specific province
app.get('/api/provinces/:provinceId/regencies', async (req, res) => {
    const { provinceId } = req.params;
    try {
        const data = await fs.readFile(path.join(__dirname, 'data', provinceId, 'regencies.json'), 'utf8');
        res.json(JSON.parse(data));
    } catch (error) {
        res.status(404).json({ error: 'Regencies not found for this province' });
    }
});

// API endpoint for districts in a specific regency
app.get('/api/provinces/:provinceId/regencies/:regencyId/districts', async (req, res) => {
    const { provinceId, regencyId } = req.params;
    try {
        const data = await fs.readFile(path.join(__dirname, 'data', provinceId, regencyId, 'district.json'), 'utf8');
        res.json(JSON.parse(data));
    } catch (error) {
        res.status(404).json({ error: 'Districts not found for this regency' });
    }
});

// API endpoint for villages (subdistricts) in a specific district
app.get('/api/provinces/:provinceId/regencies/:regencyId/districts/:districtId/villages', async (req, res) => {
    const { provinceId, regencyId, districtId } = req.params;
    try {
        const data = await fs.readFile(path.join(__dirname, 'data', provinceId, regencyId, districtId, 'subdistrict.json'), 'utf8');
        res.json(JSON.parse(data));
    } catch (error) {
        res.status(404).json({ error: 'Villages not found for this district' });
    }
});

app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});