httpsrv - http Server#
Adapted Air780E Air780EP
Note
This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!
Tip
This library has its own demo,click this link to view httpsrv demo examples
httpsrv.start(port, func)#
Start and listen on an http port
Parameters
Incoming Value Type |
Explanation |
---|---|
int |
Port number |
function |
callback function |
Return Value
return value type |
explanation |
---|---|
bool |
Returns true on success, otherwise false |
Examples
-- Listening on port 80
httpsrv.start(80, function(client, method, uri, headers, body)
-- method is a string, such GET POST PUT DELETE
-- uri is also a string such / /api/abc
-- headers table Type
-- body String
log.info("httpsrv", method, uri, json.encode(headers), body)
if uri == "/led/1" then
LEDA(1)
return 200, {}, "ok"
elseif uri == "/led/0" then
LEDA(0)
return 200, {}, "ok"
end
-- Conventions for returning values code, headers, body
-- If there is no return value, the default 404, {} ,""
return 404, {}, "Not Found" .. uri
end)
-- About Static Files
-- Case 1: / , mapping /index.html
-- Case 2: /abc.html, look for/abc.html first, if it doesn't exist /abc.html.gz
-- if gz exists, it will automatically respond with a compressed file. most browsers support it..
-- Currently, files under/luadb/xxx are found by default and cannot be configured for the time being.
httpsrv.stop(port)#
Stop the http service
Parameters
Incoming Value Type |
Explanation |
---|---|
int |
Port number |
Return Value
return value type |
explanation |
---|---|
nil |
No current return value |
Examples
None