Skip to main content

HttpServer

Description:

  Represents an HTTP server that can handle requests and serve files.

localIP​

Type: Readonly Field.

Description:

  The local IP address of the server.

Signature:

const localIP: string

wsConnectionCount​

Type: Readonly Field.

Description:

  The number of WebSocket connections.

Signature:

const wsConnectionCount: integer

wwwPath​

Type: Field.

Description:

  The path to the root static file directory of the server.

Signature:

wwwPath: string

start​

Type: Function.

Description:

  Starts the HTTP server on the specified port.

Signature:

start: function(self: HttpServer, port: integer): boolean

Parameters:

ParameterTypeDescription
portintegerThe port number on which to start the server.

Returns:

Return TypeDescription
booleanA boolean value indicating whether the server started successfully.

startWS​

Type: Function.

Description:

  Starts the WebSocket server on the specified port.

Signature:

startWS: function(self: HttpServer, port: integer): boolean

Parameters:

ParameterTypeDescription
portintegerThe port number on which to start the server.

Returns:

Return TypeDescription
booleanA boolean value indicating whether the server started successfully.

get​

Type: Function.

Description:

  Registers a handler function for handling GET requests.

Signature:

get: function(
self: HttpServer,
pattern: string,
handler: function(Request): (table | string)
)

Parameters:

ParameterTypeDescription
patternstringThe pattern to match the URL of the request.
handlerfunctionThe handler function to call when the pattern is matched.
The function can return a Lua table which will be serialized to JSON, or return a string as plain text.

post​

Type: Function.

Description:

  Registers a handler function for handling POST requests.

Signature:

post: function(
self: HttpServer,
pattern: string,
handler: function(Request): table
)

Parameters:

ParameterTypeDescription
patternstringThe pattern to match the URL of the request.
handlerfunctionThe handler function to call when the pattern is matched. The function should return a Lua table containing response data that can be serialized to JSON.

postSchedule​

Type: Function.

Description:

  Registers a handler function for handling POST requests in a coroutine.

Signature:

postSchedule: function(
self: HttpServer,
pattern: string,
handler: function(Request): table
)

Parameters:

ParameterTypeDescription
patternstringThe URL pattern to match.
handlerfunctionThe handler function to call when the pattern is matched. The function should return a dictionary containing response data that can be serialized to JSON, and the function will run in a coroutine.

upload​

Type: Function.

Description:

  Registers a handler function for handling multipart POST requests for file uploads.

Signature:

upload: function(
self: HttpServer,
pattern: string,
acceptHandler: function(req: Request, filename: string): (string | nil),
doneHandler: function(req: Request, filename: string): boolean
)

Parameters:

ParameterTypeDescription
patternstringThe URL pattern to match.
acceptHandlerfunctionThe handler function to call when the pattern is matched. The function should return the filename to save the file as, or return null to reject the file.
doneHandlerfunctionThe handler function to call when the pattern is matched. The function should return true to accept the file, or false to reject the file.

stop​

Type: Function.

Description:

  Stops the servers, including HTTP and WebSocket servers.

Signature:

stop: function(self: HttpServer)