https://github.com/leonmanolo/rex
Express inspired webserver in Rust
https://github.com/leonmanolo/rex
expressjs rest-api rust rust-crate rust-lang
Last synced: 3 months ago
JSON representation
Express inspired webserver in Rust
- Host: GitHub
- URL: https://github.com/leonmanolo/rex
- Owner: LeonManolo
- Created: 2024-11-28T12:35:21.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-12-19T12:26:19.000Z (over 1 year ago)
- Last Synced: 2025-04-07T06:43:54.783Z (about 1 year ago)
- Topics: expressjs, rest-api, rust, rust-crate, rust-lang
- Language: Rust
- Homepage:
- Size: 29.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rex Webserver
Rex is express.js inspired. Rex is a simple, lightweight, and extensible web server written in Rust. It allows you to define routes and handle HTTP requests with ease. This README provides an overview of the Rex web server, its features, and how to get started.
## Features
- **Routing**: Define routes for different HTTP methods (GET, POST, DELETE, etc.).
- **Path Parameters**: Extract parameters from the URL path.
- **Query Parameters**: Access query parameters from the URL.
- **Multithreading**: Handle multiple requests concurrently using threads.
- **Custom Responses**: Send custom text or JSON responses.
## Getting Started
### Prerequisites
- Rust (latest stable version)
### Installation
Clone the repository:
```sh
git clone https://github.com/yourusername/rex-webserver.git
cd rex-webserver
```
### Usage
Define your routes and start the server in the main.rs file.
```rs
app.get("/users/:id", |request, response| {
println!("Hallo von /users/:id");
println!("body: {}", request.body);
let my_custom_query_param = request.query_params.get("myparam");
let my_custom_query_param2 = request.query_params.get("myotherparam");
let id = request.param("id");
response.set_header("my-custom-header", "custom-header-value");
let response_text = format!(
"My awesome body! myparam: {} and myotherparam: {} and my param path param id is = {}",
my_custom_query_param.unwrap_or(&String::new()),
my_custom_query_param2.unwrap_or(&String::new()),
id.unwrap_or(String::new()),
);
return response.send_text(response_text.as_str());
});
```
### Running the Server
To run the server, use the following command:
```sh
cargo run
```
The server will start and listen on 127.0.0.1:8080.
#### Example Requests
- GET /users/:id: Handles requests to /users/:id and extracts the id parameter.
- POST /courses/:id: Handles POST requests to /courses/:id.