S42Core Server Class
- Server Class Documentation
- Overview
- Features
- Constructor
- Methods
- start(properties: TypeServerConstructor): Promise
- getPort(): number | undefined
- getURL(): string | undefined
- isStartedFromCluster(): boolean
- getClusterName(): string
- sendMessageToCluster(message: string): void
- sendMessageToWorkers(message: string): void
- onMessageFromWorkers(callback: (message: string) => void): void
- Example Implementation
- Static Content
- Error Handling
- Cluster Communication
- Notes
- License
Server Class Documentation
Overview
The Server
class is a robust implementation for managing HTTP servers using Bun.js. It supports clustering, hooks, customizable request handling, and error management. This documentation provides an overview of its features, configuration options, and an example implementation.
Features
- Clustering Support: Distributes load across multiple processes.
- Hooks: Allows adding custom hooks to extend functionality.
- Error Handling: Customizable error responses.
- Dynamic Routing: Integrates with
RouteControllers
for handling routes. - Cluster Communication: Facilitates communication between workers and clusters.
- Health Check: Includes a static
/health-check
endpoint. - Configurable Settings: Supports customization of timeout, body size, and development options.
Constructor
- Initializes the
Server
instance and listens for cluster communication messages.
Methods
start(properties: TypeServerConstructor): Promise
Starts the server with the specified configuration.
Parameters:
- port (number): The port on which the server listens. Default is
0
. - clustering (boolean): Enables port reuse for clustering. Default is
false
. - idleTimeout (number): The maximum idle time in seconds before a connection is closed. Default is
300
. - maxRequestBodySize (number): The maximum size (in bytes) of the request body. Default is
1,000,000
. - error (function): Custom error handler. Receives an
Error
object and returns aResponse
. - hooks (TypeHook[]): Array of hooks to execute during server operations.
- RouteControllers (RouteControllers): Handles routes and their corresponding logic.
- development (boolean): Indicates if the server is running in development mode. Default is
false
. - awaitForCluster (boolean): Delays server startup until the cluster communication is established. Default is
false
.
Example:
getPort(): number | undefined
Returns the port on which the server is running.
getURL(): string | undefined
Returns the full URL of the server.
isStartedFromCluster(): boolean
Indicates if the server was started as part of a cluster.
getClusterName(): string
Returns the name of the cluster the server belongs to.
sendMessageToCluster(message: string): void
Sends a message to the parent cluster.
sendMessageToWorkers(message: string): void
Sends a message to all worker processes.
onMessageFromWorkers(callback: (message: string) => void): void
Registers a callback to handle messages received from worker processes.
Example Implementation
Below is an example of how to use the Server
class along with RouteControllers
:
Static Content
- The server includes a default static route for health checks:
- URL:
/health-check
- Response:
"All good!"
- URL:
Error Handling
Custom error responses can be defined via the error
property:
Cluster Communication
The server supports communication between clusters and workers. Example:
Sending a Message to the Cluster
Receiving Messages from Workers
Notes
- Ensure the
RouteControllers
instance is properly configured to handle routes. - Use the
clustering
option for high availability and scalability. - Utilize hooks for adding middleware or pre-processing logic.
License
This project is licensed under the MIT License. For more information, see the LICENSE file.