Multi-Server Configuration
Run multiple server instances with different configurations from a single setup.
Overview
XyPriss supports running multiple server instances with different configurations, ports, and route scopes. This is perfect for:
- Microservices architectures
- API versioning
- Separating admin panels from public APIs
- Different security levels per service
Basic Setup
import { createServer } from 'xypriss';
const app = createServer({
multiServer: {
enabled: true,
servers: [
{
id: "api-server",
port: 3001,
routePrefix: "/api",
allowedRoutes: ["/api/*"]
},
{
id: "admin-server",
port: 3002,
routePrefix: "/admin",
allowedRoutes: ["/admin/*"],
security: { level: "maximum" }
}
]
}
});
// Routes are automatically distributed
app.get("/api/users", (req, res) => res.json({ service: "api" }));
app.get("/admin/dashboard", (req, res) => res.json({ service: "admin" }));
// Start all servers
await app.startAllServers();
Configuration Options
Server Configuration
interface MultiServerConfig {
id: string; // Unique identifier
port: number; // Port number
host?: string; // Host (optional)
routePrefix?: string; // Route prefix
allowedRoutes?: string[]; // Route patterns
// Server-specific overrides
server?: object;
security?: SecurityConfig;
cache?: CacheConfig;
performance?: PerformanceConfig;
}
Route Filtering
Route Prefix
{
id: "api-server",
port: 3001,
routePrefix: "/api/v1"
// Handles routes starting with /api/v1
}
Allowed Routes
{
id: "admin-server",
port: 3002,
allowedRoutes: ["/admin/*", "/dashboard"]
// Handles /admin/* and /dashboard routes
}
Use Cases
API Versioning
multiServer: {
enabled: true,
servers: [
{
id: "api-v1",
port: 3001,
routePrefix: "/api/v1",
allowedRoutes: ["/api/v1/*"]
},
{
id: "api-v2",
port: 3002,
routePrefix: "/api/v2",
allowedRoutes: ["/api/v2/*"]
}
]
}
Microservices Separation
multiServer: {
enabled: true,
servers: [
{
id: "user-service",
port: 3001,
allowedRoutes: ["/users/*", "/auth/*"]
},
{
id: "product-service",
port: 3002,
allowedRoutes: ["/products/*", "/categories/*"]
},
{
id: "order-service",
port: 3003,
allowedRoutes: ["/orders/*", "/cart/*"]
}
]
}
Best Practices
- Use meaningful server IDs for easy identification
- Group related routes on the same server
- Configure appropriate security levels for each server
- Monitor resource usage of each server instance
- Use reverse proxy in production for load balancing
- Implement health checks for each server