Because C deserves frameworks as simple as Express.js
A lightweight HTTP server framework for C that brings the simplicity of Express.js to the power and performance of C.
CExpress is a minimal, high-performance HTTP server framework written in C that provides Express.js-like functionality for building web applications and APIs. Perfect for microservices, APIs, static file serving, and rapid prototyping without the overhead of larger frameworks.
π Full documentation available at: CExpress Docs
- Overview
- Features
- Installation
- Quick Start
- Examples
- API Reference
- Architecture
- Performance
- Contributing
CExpress brings the familiar Express.js patterns to C development:
- Rapid Prototyping: Build and test instantly
- Simple routing: GET, POST, PUT, DELETE
- Handler-based architecture: Modular, clean code
- Static file serving: Out of the box
- JSON API support: Built-in response formatting
- Cross-platform: Linux and macOS
- Lightweight: Minimal memory footprint and dependencies
- Fast: Built on efficient C socket programming
- Simple: Express.js-inspired API design
- Flexible: Support for various use cases and patterns
- HTTP/1.1 compliant request/response handling
- Dynamic routing with method and path matching
- Memory management with automatic cleanup
- Error handling with proper HTTP status codes
- Signal handling for graceful shutdown
- Concurrent client support with select() multiplexing
- REST APIs and microservices
- Static file hosting
- Web application backends
- Rapid development servers
- Prototyping and testing
- Embedded web interfaces
CExpress can be installed as a shared C library in two ways:
- GCC or Clang compiler
- Make utility
git clone https://github.com/Karl-Michaud/CExpress.git
cd CExpress
make installThis will:
- Copy the shared library (
libCExpress.dylibon macOS, orlibCExpress.soon Linux) to/usr/local/lib/ - Copy the header files to
/usr/local/include/CExpress/
To remove CExpress, run:
make uninstall- To install to a different location, set the
PREFIXvariable:make install PREFIX=/custom/path
- For Linux, the shared library extension is
.so; for macOS, it is.dylib.
brew tap Karl-Michaud/cexpress
brew install cexpress(Optional) Verify installation:
gcc -o test test.c -lCExpress
./testbrew uninstall cexpress(Optional) Remove the tap and clear cached downloads:
brew untap Karl-Michaud/cexpress
brew cleanupThis will remove the CExpress library and headers installed by Homebrew.
#include <CExpress/server.h>
#include <stdio.h>
#include <stdlib.h>
char *hello_handler(void) {
char *response = malloc(50);
strcpy(response, "Hello from CExpress!");
return response;
}
int main(void) {
// Initialize server
Server *server = server_init(8080, 10, 5, DEV);
if (!server) {
fprintf(stderr, "Failed to initialize server\n");
return 1;
}
// Add route
server_add_route(server, GET, "/hello", hello_handler);
// Start server
server_start(server);
// Cleanup (automatic on shutdown)
return 0;
}gcc -o server main.c -lCExpress
./serverTest with:
curl http://localhost:8080/helloFound in examples/
- Hello World β minimal GET route
- JSON API β Multi-method API with JSON responses, error handling, and data management.
- Static Files β serve HTML, CSS, JS
- REST API β Full CRUD operations with proper REST patterns and status codes.
// Initialize server
Server *server_init(int port, int max_clients, int backlog, Mode mode);
// Start server (blocks until shutdown)
int server_start(Server *server);
// Clean up resources
void server_free(Server *server);// Add route
int server_add_route(Server *server, method_t method, path_t path, HandlerFunc handler);
// Remove route
int server_remove_route(Server *server, method_t method, path_t path);// Handler function signature
typedef char *(*HandlerFunc)(void);
// Example handler
char *my_handler(void) {
char *response = malloc(100);
strcpy(response, "Hello World!");
return response;
}typedef enum {GET, POST, PUT, DELETE, FAIL} method_t;typedef enum {DEV, PROD} Mode;
// DEV - localhost only (127.0.0.1)
// PROD - all interfaces (0.0.0.0)CExpress follows a modular architecture:
βββββββββββββββββββ
β Public API β β server.h
βββββββββββββββββββ€
β Server Core β β server.c
βββββββββββββββββββ€
β Routing β β routers.c/h
βββββββββββββββββββ€
β Handlers β β handlers.c/h
βββββββββββββββββββ€
β Utilities β β utils.c/h
βββββββββββββββββββ
- Client Request β Server Socket
- Accept Connection β Client List
- Read Data β Buffer
- Parse Header β Extract Method/Path
- Find Route β Router List
- Execute Handler β Generate Response
- Send Response β Client
- Cleanup β Memory Management
CExpress is designed for performance:
- Low Latency: Direct socket programming
- Memory Efficient: Minimal allocations and automatic cleanup
- Concurrent: Multiple client support
- Lightweight: Small binary size and memory footprint
- Rapid Prototyping: Instantly build and test web applications without the overhead of large frameworks
I welcome contributions! Here's how to get started:
- Fork the repository & create a feature branch
- Make your changes & add tests if applicable
- Submit a PR π
- Additional HTTP methods
- Middleware support
- WebSocket support
- Enhanced error handling
- Performance optimizations
- Additional examples
Made by Karl-Alexandre Michaud
Because C deserves frameworks as simple as Express.js