Rust HTTP Server
A lightweight HTTP server implementation in Rust, inspired by Express.js. This project implements core HTTP functionality with a clean, Express-like API.
Features
- Full HTTP/1.1 protocol support
- Response types:
- Plain text
- JSON
- Static files
- HTML templates
- Cookie management
- Standard HTTP methods (GET, POST, PUT, DELETE, PATCH)
- Error handling
- Query parameter parsing
- Modular project structure
- Built from scratch network implementation
- Async/sync route handlers
Getting Started
Clone the repository:
Setup a basic server
use App;
use ;
async
Run the server:
Usage Guide
Basic Route Setup
Routes are defined in src/main.rs. Here's how to work with different types of handlers:
Working with Request Data
// Query parameters
router.get;
// Request body (POST/PUT)
router.post;
File Operations
// Serve static files
router.get;
// Render HTML templates
router.get;
Working with Cookies
router.get;
Error Handling
router.get;
Response Types
The server supports multiple response types:
- Text:
response.text("Hello", 200) - JSON:
response.json(json!({"key": "value"}), 200) - HTML:
response.render("template.html", 200) - File:
response.send_file("file.pdf", 200) - Error:
response.error("Error message", 500)
HTTP Methods
All standard HTTP methods are supported:
router.get;
router.post;
router.put;
router.patch;
router.delete;
Each method also has an async version (e.g., get_async, post_async) for handling asynchronous operations.
Example: Main File Setup
In main.rs, initialize and run the application as follows:
use App;
use ;
async
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.