Module jls.net.http.HttpServer

An HTTP server implementation that handles HTTP requests.

Class HttpServer

HttpServer:new () Creates a new HTTP server.
httpServer:bind ([node[, port[, backlog[, callback]]]]) Binds this server to the specified address and port number.
httpServer:close ([callback]) Closes this server.


Class HttpServer

An HTTP server. The HttpServer inherits from HttpContextHolder.

Usage:

local event = require('jls.lang.event')
local HttpServer = require('jls.net.http.HttpServer')
local hostname, port = '::', 3001
local httpServer = HttpServer:new()
httpServer:bind(hostname, port):next(function()
  print('Server bound to "'..hostname..'" on port '..tostring(port))
end, function(err) -- could failed if address is in use or hostname cannot be resolved
  print('Cannot bind HTTP server, '..tostring(err))
end)
httpServer:createContext('/', function(httpExchange)
  local response = httpExchange:getResponse()
  response:setBody('It works !')
end)
event:loop()
HttpServer:new ()
Creates a new HTTP server.

Returns:

    a new HTTP server
httpServer:bind ([node[, port[, backlog[, callback]]]])
Binds this server to the specified address and port number.

Parameters:

  • node string the address, the address could be an IP address or a host name. (optional)
  • port number the port number, 0 to let the system automatically choose a port, default is 80. (optional)
  • backlog number the accept queue size, default is 32. (optional)
  • callback function an optional callback function to use in place of promise. (optional)

Returns:

    jls.lang.Promise a promise that resolves once the server is bound.

Usage:

    local s = HttpServer:new()
    s:bind('127.0.0.1', 80)
httpServer:close ([callback])
Closes this server.

Parameters:

  • callback function an optional callback function to use in place of promise. (optional)

Returns:

    jls.lang.Promise a promise that resolves once the server is closed.
generated by LDoc 1.4.6 Last updated 2022-01-22 16:32:56