cHttpServer Methods Reference
🚀 Server Control Methods
Start
Starts HTTP server.
vb
Public Function Start(Optional Port As Long = 80, Optional WebRoot As String = "", Optional IP As String = "0.0.0.0") As BooleanParameters:
Port- Listen port (default 80)WebRoot- Static file root directoryIP- Listen IP address (default 0.0.0.0 means all interfaces)
Returns: Returns True on success, False on failure
Example:
vb
' Basic start
If Server.Start(8080) Then
Debug.Print "Server started successfully"
End If
' With static file directory
If Server.Start(8080, "C:\WebRoot") Then
Debug.Print "Server started successfully"
End If
' Specify IP
If Server.Start(8080, "C:\WebRoot", "127.0.0.1") Then
Debug.Print "Local server started successfully"
End IfStopMe
Stops HTTP server.
vb
Public Function StopMe() As BooleanDescription: Closes all connections, releases resources.
Example:
vb
Private Sub Form_Unload(Cancel As Integer)
Server.StopMe
Set Server = Nothing
End Sub🛣️ Route-Related Methods (via Router Object)
Reg
Registers a controller.
vb
Public Function Reg(ControllerName As String, Controller As Object) As BooleanParameters:
ControllerName- Controller nameController- Controller object instance
Example:
vb
' Register controllers
Call Server.Router.Reg("User", New cUserController)
Call Server.Router.Reg("Api", New cApiController)Add
Adds route rule.
vb
Public Function Add(RouteName As String, Handler As String, Optional MethodLimit As EnumRouteMethod = Any_) As BooleanParameters:
RouteName- Route path (e.g., "/user")Handler- Handler (format: "ControllerName@MethodName")MethodLimit- HTTP method limit:Any_- Any method (default)OnlyGet- GET onlyOnlyPost- POST onlyOnlyPut- PUT onlyOnlyDelete- DELETE only
Example:
vb
' Basic routes
Call Server.Router.Add("/", "Home@Index")
Call Server.Router.Add("/user", "User@List", OnlyGet)
Call Server.Router.Add("/user/create", "User@Create", OnlyPost)
Call Server.Router.Add("/user/update", "User@Update", OnlyPut)
Call Server.Router.Add("/user/delete", "User@Delete", OnlyDelete)📡 Events
OnAccept
Triggers when new connection is accepted.
vb
Public Event OnAccept(ClientInfo As cHttpServerClientInfo, Disconnect As Boolean)Parameters:
ClientInfo- Client info objectDisconnect- Set to True to reject connection
Example:
vb
Private Sub Server_OnAccept(ClientInfo As cHttpServerClientInfo, Disconnect As Boolean)
Debug.Print "New connection: " & ClientInfo.IP & ":" & ClientInfo.Port
' IP blacklist check
If ClientInfo.IP = "192.168.1.100" Then
Disconnect = True ' Reject connection
End If
End SubOnLogs
Log event.
vb
Public Event OnLogs(ByVal Level As String, ByVal Content As String)Example:
vb
Private Sub Server_OnLogs(ByVal Level As String, ByVal Content As String)
Debug.Print "[" & Level & "] " & Content
End Sub🔧 Controller Method Writing Specification
Controller methods receive one context parameter containing request and response objects:
vb
Public Sub ActionName(ctx As cHttpServerContext)
' ctx.Request - Request object
' ctx.Response - Response object
' ctx.Session - Session object
' ctx.Cookies - Cookies object
' ctx.Db - Database object (if configured)
End SubExample Controller:
vb
' cUserController.cls
Option Explicit
' GET /user
Public Sub List(ctx As cHttpServerContext)
Dim users As New Scripting.Dictionary
users("items") = Array("John", "Jane", "Bob")
users("total") = 3
ctx.Response.Json users, 0, "Success"
End Sub
' POST /user/create
Public Sub Create(ctx As cHttpServerContext)
' Get POST data
Dim username As String
username = ctx.Request.Form("username")
' Get JSON data
' username = ctx.Request.Json.GetItem("username")
ctx.Response.Json Nothing, 0, "Created successfully"
End Sub
' GET /user?id=1
Public Sub Detail(ctx As cHttpServerContext)
Dim id As String
id = ctx.Request.QueryString("id")
Dim user As New Scripting.Dictionary
user("id") = id
user("name") = "John"
ctx.Response.Json user
End Sub📦 Context Object Properties
cHttpServerContext
| Property | Type | Description |
|---|---|---|
Request | cHttpServerRequest | Request object |
Response | cHttpServerResponse | Response object |
Session | cHttpServerSession | Session object |
Cookies | cHttpServerCookies | Cookies object |
Db | cDataBase | Database object |
Server | cHttpServerSvr | Server config |
ClientInfo | cHttpServerClientInfo | Client info |
Last Updated: 2026-05-17