HTTP Server Examples
This directory contains examples of building HTTP servers with Duso.
Quick Start
duso examples/http/server.du
Then visit:
- http://localhost:8080/ - Home page
- http://localhost:8080/hello - Simple greeting
- http://localhost:8080/hello?name=YourName - Greeting with parameter
- http://localhost:8080/api/time - JSON API
Press Ctrl+C to stop the server gracefully.
Files
How It Works
- The main script creates an HTTP server on port 8080
- Routes are registered with the handler script paths
server.start() blocks, listening for requests
- Each incoming request spawns a fresh script instance
- The handler script accesses the request via
context() builtin
- When Ctrl+C is pressed, the server shuts down gracefully
- Cleanup code in the main script runs after shutdown
Key Features Demonstrated
- Multiline strings - Using
"""...""" for clean HTML/JSON
- String templates - Using
{{...}} to embed variables
- Request context - Using
context() to access request data
- Query parameters - Accessing
req.query["name"]
- Response handling - Building response objects with status, headers, body
- Graceful shutdown - Cleanup code runs when server stops
Exploring Further
Try modifying:
- Add new routes in
server.du
- Create new handler scripts
- Use query parameters and request headers
- Return different content types (JSON, HTML, plain text, etc.)