Skip to content

Simple Server ​

Start a minimal server and return JSON from a single route.

Demo Program ​

zig
const std = @import("std");
const httpx = @import("httpx");

fn health(ctx: *httpx.Context) anyerror!httpx.Response {
    return ctx.renderJson(.{ .ok = true, .service = "demo" });
}

pub fn main() !void {
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();
    const io = std.Io.Threaded.global_single_threaded.io();

    var server = try httpx.Server.init(allocator, io, .{
        .host = "127.0.0.1",
        .port = 8080,
        .portStrategy = .incremental,
        .maxPortAttempts = 32,
        .maxConnections = 1000,
        .keepAlive = true,
    });
    defer server.deinit();

    try server.get("/health", health);
    server.run();
}

Run ​

bash
zig build run-simple-server

What to Verify ​

  • GET /health returns JSON response.
  • Server starts without route registration errors.
  • If 8080 is occupied, server startup can automatically move to the next port based on config.
  • Browser request to http://127.0.0.1:<effective-port>/health returns immediately.

Released under the MIT License.