-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (44 loc) · 1.44 KB
/
index.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
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const { recursiveZipCodeSearch } = require('./zip');
const port = 3000;
const filePath = path.join(__dirname, 'ar-zip-codes.txt');
function loadZipDataFile(filePath) {
const zipData = {};
const data = fs.readFileSync(filePath, 'utf-8');
const lines = data.split('\n');
lines.forEach(line => {
const row = line.split('\t');
if (row.length >= 9) {
const zipCode = row[1];
const lat = parseFloat(row[9]);
const lon = parseFloat(row[10]);
zipData[zipCode] = { zip: row[1], lat, lon, loc: `${row[2]}, ${row[3]}`};
}
});
return zipData;
}
// Function to load zip data
function loadZipData(filePath) {
const zipCode = 1636;
const zipData = loadZipDataFile(filePath);
const zips = recursiveZipCodeSearch(zipCode, zipData);
return zips.map(zip => zipData[zip])
}
const zipData = loadZipData(filePath);
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Route for serving the HTML file
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Endpoint to get zip data
app.get('/api/zip-data', (req, res) => {
res.json(zipData);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});