Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
88 lines (76 loc) · 3.26 KB
/
Copy pathserver.js
File metadata and controls
88 lines (76 loc) · 3.26 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// שרת Node מינימלי (ללא תלויות) שמגיש את ה-UI ומשמש פרוקסי ל-API של דואר ישראל.
// דורש Node 18+ (לשימוש ב-fetch המובנה). הרצה: npm start
'use strict';
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = process.env.PORT || 3000;
const API_BASE = 'https://apimftprd.israelpost.co.il/mypost-zip';
// המפתח מוטמע גלוי בפרונטאנד של דואר ישראל. ניתן לעקוף עם משתנה סביבה IL_POST_KEY.
const SUB_KEY = process.env.IL_POST_KEY || '5ccb5b137e7444d885be752eda7f767a';
// קריאת GET ל-API של דואר ישראל, מחזירה את גוף התגובה כמחרוזת JSON.
async function apiGet(endpoint, params) {
const url = new URL(API_BASE + endpoint);
for (const [k, v] of Object.entries(params)) {
if (v !== undefined && v !== null && v !== '') url.searchParams.set(k, v);
}
const res = await fetch(url, {
headers: { 'Ocp-Apim-Subscription-Key': SUB_KEY, 'Accept': 'application/json' },
});
return { status: res.status, body: await res.text() };
}
function sendJson(res, status, body) {
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' });
res.end(body);
}
// מפת ה-routes של ה-API שלנו -> קריאה לדואר ישראל
const routes = {
// /api/cities?q=תל א&lang=he
'/api/cities': (q) =>
apiGet('/getcities-lang', { CityStartsWith: q.q, Lang: q.lang || 'he' }),
// /api/streets?cityId=1212&q=דיזנגוף
'/api/streets': (q) =>
apiGet('/GetStreets-lang', {
CityID: q.cityId, CityName: '', SearchMode: 'ID-StartsWith',
StartsWith: q.q, Lang: q.lang || 'he',
}),
// /api/zip?cityId=1212&streetId=91992&house=100&entry=
'/api/zip': (q) =>
apiGet('/SearchZip-Lang', {
CityID: q.cityId, StreetID: q.streetId, House: q.house,
Entry: q.entry, Lang: q.lang || 'he', ByMaanimID: 'true',
}),
// /api/zip-pob?cityId=1212&pob=100
'/api/zip-pob': (q) =>
apiGet('/SearchZip-Lang', {
CityID: q.cityId, POB: q.pob, Lang: q.lang || 'he', ByMaanimID: 'true',
}),
};
const server = http.createServer(async (req, res) => {
const u = new URL(req.url, `http://${req.headers.host}`);
const handler = routes[u.pathname];
if (handler) {
try {
const q = Object.fromEntries(u.searchParams);
const { status, body } = await handler(q);
sendJson(res, status, body);
} catch (err) {
sendJson(res, 502, JSON.stringify({ error: String(err) }));
}
return;
}
// הגשת קבצים סטטיים מתוך public/
const file = u.pathname === '/' ? '/index.html' : u.pathname;
const filePath = path.join(__dirname, 'public', path.normalize(file).replace(/^(\.\.[/\\])+/, ''));
fs.readFile(filePath, (err, data) => {
if (err) { res.writeHead(404); res.end('Not found'); return; }
const ext = path.extname(filePath);
const types = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript', '.css': 'text/css' };
res.writeHead(200, { 'Content-Type': types[ext] || 'application/octet-stream' });
res.end(data);
});
});
server.listen(PORT, () => {
console.log(`\n דואר ישראל — איתור מיקוד`);
console.log(` http://localhost:${PORT}\n`);
});