Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Web Sunucu Örneği

Basit HTTP Sunucu

import http

işlev ana() {
    değişken sunucu = http.sunucu_oluştur()
    
    sunucu.rota("/", (istek, yanıt) => {
        yanıt.gönder("Merhaba Dünya!")
    })
    
    sunucu.rota("/api/zaman", (istek, yanıt) => {
        değişken şimdi = time.now()
        yanıt.json({ "zaman": şimdi })
    })
    
    yazdır("Sunucu başlatıldı: http://localhost:3000")
    sunucu.dinle(3000)
}

REST API

import http, json

tür Kullanıcı = struct {
    id: Sayı,
    ad: Yazı,
    email: Yazı
}

değişken kullanıcılar: Liste<Kullanıcı> = []

işlev ana() {
    değişken app = http.sunucu_oluştur()
    
    // Tüm kullanıcıları getir
    app.get("/api/kullanıcılar", (req, res) => {
        res.json(kullanıcılar)
    })
    
    // Yeni kullanıcı ekle
    app.post("/api/kullanıcılar", (req, res) => {
        değişken yeni = json.parse<Kullanıcı>(req.body)
        kullanıcılar.ekle(yeni)
        res.json(yeni)
    })
    
    app.dinle(8080)
}

WebSocket Örneği

import ws

işlev ana() {
    değişken sunucu = ws.sunucu_oluştur()
    
    sunucu.bağlantı((socket) => {
        yazdır("Yeni bağlantı")
        
        socket.mesaj((data) => {
            yazdır("Alındı: ", data)
            socket.gönder("Echo: " + data)
        })
    })
    
    sunucu.dinle(9000)
}