İçeriğe geç
Örnekler

En yakın hastane

Konumun 10 km çevresindeki hastaneler, uzaklık ve türüyle (kamu, özel, üniversite). Konum tarayıcıdan ya da elle koordinat.

  1. Anahtar al. Ücretsiz kayıt, ayda 1.000 kredi. Anahtarı panelde üretirsin; ak_live_ ile başlar.
  2. Dosyayı al. Aşağıdan kopyala ya da indir: hastane.mjs. Node 18 ya da üstü yeter, paket yok.
  3. Çalıştır. KILAVUZ_ANAHTAR=ak_live_… node hastane.mjs. Sayfa localhost:3000’de açılır.

Tek dosya, olduğu gibi çalışır:

// Hastane bulucu: konumuna en yakın hastaneler, uzaklık ve türüyle (kamu, özel, üniversite).
// Node 18+, paket yok.  KILAVUZ_ANAHTAR=ak_live_… node hastane.mjs  →  http://localhost:3000
// Anahtar yalnız bu sunucuda durur; tarayıcı aşağıdaki izinli uca gider, sunucu Kılavuz'a iletir.
import { createServer } from 'node:http';

const API = 'https://api.kilavuzapi.com';
const ANAHTAR = process.env.KILAVUZ_ANAHTAR;
if (!ANAHTAR) throw new Error('KILAVUZ_ANAHTAR tanımlı değil (anahtar: kilavuzapi.com/panel)');
const IZINLI = ['/v1/saglik/hastane'];
const PORT = process.env.PORT ?? 3000;

createServer(async (istek, yanit) => {
  const { pathname, search } = new URL(istek.url, 'http://yerel');
  if (!IZINLI.includes(pathname)) return yanit.writeHead(200, { 'content-type': 'text/html; charset=utf-8' }).end(SAYFA);
  let govde = '';
  for await (const parca of istek) if ((govde += parca).length > 10_000) return yanit.writeHead(413).end();
  try {
    const r = await fetch(API + pathname + search, {
      method: istek.method,
      headers: { authorization: `Bearer ${ANAHTAR}`, 'content-type': 'application/json' },
      body: istek.method === 'POST' ? govde : undefined,
    });
    yanit.writeHead(r.status, { 'content-type': 'application/json; charset=utf-8' }).end(await r.text());
  } catch {
    yanit.writeHead(502, { 'content-type': 'application/json; charset=utf-8' }).end('{"hata":"Kılavuz API\'sine ulaşılamadı"}');
  }
}).listen(PORT, '127.0.0.1', () => console.log(`http://localhost:${PORT}`));

const SAYFA = `<!doctype html>
<html lang="tr">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>En yakın hastane</title>
<style>
  body { font: 16px/1.5 system-ui, sans-serif; max-width: 30rem; margin: 40px auto; padding: 0 16px; color: #16181c; }
  form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  input, select { box-sizing: border-box; width: 100%; padding: 8px 10px; font: inherit; border: 1px solid #c3c7cd; border-radius: 6px; }
  button { padding: 10px 14px; font: inherit; border: 0; border-radius: 6px; background: #16181c; color: #fff; cursor: pointer; }
  #konum { background: #e7e9ec; color: #16181c; }
  ol { margin: 24px 0 0; padding: 0; list-style: none; }
  li { padding: 12px 0; border-top: 1px solid #dfe2e6; }
  li b { display: block; }
  small, #durum { font-size: 13px; color: #6b7280; }
</style>
<h1>En yakın hastane</h1>
<form id="form">
  <input name="lat" value="40.9862" aria-label="Enlem" inputmode="decimal">
  <input name="lon" value="29.0260" aria-label="Boylam" inputmode="decimal">
  <select name="tur" aria-label="Tür">
    <option value="">Hepsi</option>
    <option value="kamu">Kamu</option>
    <option value="ozel">Özel</option>
    <option value="universite">Üniversite</option>
  </select>
  <button>Ara</button>
  <button type="button" id="konum">Konumumu kullan</button>
</form>
<p id="durum"></p>
<ol id="liste"></ol>
<script>
  const form = document.getElementById('form');
  const liste = document.getElementById('liste');
  const durum = document.getElementById('durum');
  const TUR = { kamu: 'kamu', ozel: 'özel', universite: 'üniversite', bilinmiyor: 'türü bilinmiyor' };

  form.onsubmit = async (e) => {
    e?.preventDefault();
    const q = new URLSearchParams({ lat: form.lat.value, lon: form.lon.value, yaricap: 10, limit: 10 });
    if (form.tur.value) q.set('tur', form.tur.value);
    durum.textContent = 'Aranıyor…';
    const r = await fetch('/v1/saglik/hastane?' + q);
    const j = await r.json();
    liste.textContent = '';
    if (!r.ok) return (durum.textContent = j.hata);
    durum.textContent = j.hastaneler.length ? j.kapsam : '10 km içinde kayıt yok.';

    for (const h of j.hastaneler) {
      const li = document.createElement('li');
      const ad = document.createElement('b');
      const bilgi = document.createElement('small');
      const harita = document.createElement('a');
      ad.textContent = h.ad;
      bilgi.textContent = h.uzaklikKm.toLocaleString('tr-TR') + ' km · ' + TUR[h.tur] + (h.dal ? ' · ' + h.dal : '') + ' · ' + (h.ilce ?? h.il) + ' ';
      harita.href = 'https://www.openstreetmap.org/?mlat=' + h.lat + '&mlon=' + h.lon + '#map=17/' + h.lat + '/' + h.lon;
      harita.textContent = 'haritada aç';
      bilgi.append(harita);
      li.append(ad, bilgi);
      liste.append(li);
    }
  };

  document.getElementById('konum').onclick = () => {
    durum.textContent = 'Konum bekleniyor…';
    navigator.geolocation.getCurrentPosition(
      (k) => { form.lat.value = k.coords.latitude.toFixed(5); form.lon.value = k.coords.longitude.toFixed(5); form.onsubmit(); },
      () => { durum.textContent = 'Konum izni verilmedi; koordinatı elle yaz.'; },
    );
  };
  form.onsubmit();
</script>
`;

Kullandığı uçlar

  • GET /v1/saglik/hastaneücretsiz

Anahtar tarayıcıya inmez. Dosya küçük bir sunucu: sayfayı verir, tarayıcının isteğini anahtarı ekleyerek Kılavuz’a iletir. Yalnız IZINLI listesindeki uçlar geçer. Kendi uygulamana taşırken de anahtarı sunucuda tut. Uçların ayrıntısı dokümanda.

Öteki örnekler