A lightweight, modern HTTP server for Deno with built-in routing, middleware support, and TypeScript integration.
- π Lightweight and fast
- π Built-in security features (CORS, rate limiting, security headers)
- π― TypeScript support with full type safety
- π Middleware support
- π£οΈ Dynamic routing with URL parameters
- π Comprehensive documentation
- π§ͺ Easy to test and extend
deno add xebec-serverimport { XebecServer, cors, securityHeaders, rateLimit } from "xebec-server";
const server = new XebecServer({
debug: true,
maxBodySize: 1024 * 1024, // 1MB
defaultHeaders: {
"X-Powered-By": "Xebec"
}
});
// Add global middleware
server.use(cors({
origin: "https://example.com",
methods: ["GET", "POST"],
credentials: true
}));
server.use(securityHeaders());
server.use(rateLimit({ windowMs: 15 * 60 * 1000, max: 100 }));
// Define routes
server.GET("/", (req) => ResponseHelper.text("Hello World!"));
server.GET("/user/:id", (req) => {
return ResponseHelper.json({ id: req.params.id });
});
server.POST("/api/data", async (req) => {
const data = await req.json();
return ResponseHelper.json({ success: true, data });
}, {
options: {
parseJson: true,
validate: (req) => {
// Add custom validation
return true;
}
}
});
// Start the server
await server.listen({ port: 8000 });The main server class that handles routing and middleware.
interface ServerOptions {
debug?: boolean; // Enable detailed request logging
maxBodySize?: number; // Maximum request body size in bytes
defaultHeaders?: Record<string, string>; // Default response headers
errorHandler?: (error: Error, req: Req) => Response | Promise<Response>; // Custom error handler
}use(middleware: Middleware): Add global middlewareGET(path: string, handler: Handler, config?: RouteConfig): Define a GET routePOST(path: string, handler: Handler, config?: RouteConfig): Define a POST routePUT(path: string, handler: Handler, config?: RouteConfig): Define a PUT routeDELETE(path: string, handler: Handler, config?: RouteConfig): Define a DELETE routePATCH(path: string, handler: Handler, config?: RouteConfig): Define a PATCH routeOPTIONS(path: string, handler: Handler, config?: RouteConfig): Define an OPTIONS routeroute(prefix: string, instance: XebecServer): Mount a nested server instance
Built-in middleware functions for common use cases:
cors(options: CorsOptions): Handle Cross-Origin Resource SharingsecurityHeaders(): Add security-related response headersrateLimit(options: RateLimitOptions): Implement rate limiting
Helper functions for creating common response types:
json(data: unknown, status?: number, headers?: Record<string, string>): Create JSON responsetext(text: string, status?: number, headers?: Record<string, string>): Create text responseerror(message: string, status?: number, headers?: Record<string, string>): Create error responseredirect(url: string, status?: number): Create redirect response
- Always use HTTPS in production
- Implement proper authentication and authorization
- Validate and sanitize all user input
- Use rate limiting to prevent abuse
- Keep dependencies up to date
- Follow security best practices for your use case
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see LICENSE file for details