-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (56 loc) · 1.82 KB
/
server.js
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
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import express from "express";
import { config } from "dotenv";
import cors from "cors";
config();
const app = express();
// ✅ Required for Vercel deployment
export default app;
// ✅ Convert `import.meta.url` to a proper file path
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// ✅ Define the file paths correctly
const districtFilePath = path.join(__dirname, "district.json");
const divisionFilePath = path.join(__dirname, "division.json");
const postcodeFilePath = path.join(__dirname, "postcode.json");
const upazilasFilePath = path.join(__dirname, "upazila.json");
// ✅ Function to safely read JSON files
const readJSONFile = (filePath) => {
try {
return JSON.parse(fs.readFileSync(filePath, "utf8"));
} catch (error) {
console.error(`Error reading file ${filePath}:`, error.message);
return null;
}
};
// ✅ Read JSON files safely
const district = readJSONFile(districtFilePath);
const division = readJSONFile(divisionFilePath);
const postcode = readJSONFile(postcodeFilePath);
const upazilas = readJSONFile(upazilasFilePath);
// ✅ Middleware
app.use(cors());
app.use(express.json()); // Optional for handling JSON requests
// ✅ Routes
app.get("/", (req, res) => {
return res.status(200).json({
districts: district || [],
divisions: division || [],
postcodes: postcode || [],
upazilas: upazilas || [],
});
});
app.get("/api/bd.district", (req, res) => {
return res.status(200).json(district);
});
app.get("/api/bd.division", (req, res) => {
return res.status(200).json(division);
});
app.get("/api/bd.postcode", (req, res) => {
return res.status(200).json(postcode);
});
app.get("/api/bd.upazilas", (req, res) => {
return res.status(200).json(upazilas);
});