2015-01-19 18:12:06 +01:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
-- HTTP server Hello world example
|
|
|
|
--
|
|
|
|
-- LICENCE: http://opensource.org/licenses/MIT
|
|
|
|
-- Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
------------------------------------------------------------------------------
|
2018-12-19 12:23:14 +01:00
|
|
|
require("httpserver").createServer(80, function(req, res)
|
2015-01-19 18:12:06 +01:00
|
|
|
-- analyse method and url
|
2015-01-19 21:32:16 +01:00
|
|
|
print("+R", req.method, req.url, node.heap())
|
2015-01-19 18:12:06 +01:00
|
|
|
-- setup handler of headers, if any
|
|
|
|
req.onheader = function(self, name, value)
|
|
|
|
-- print("+H", name, value)
|
|
|
|
-- E.g. look for "content-type" header,
|
|
|
|
-- setup body parser to particular format
|
|
|
|
-- if name == "content-type" then
|
|
|
|
-- if value == "application/json" then
|
|
|
|
-- req.ondata = function(self, chunk) ... end
|
|
|
|
-- elseif value == "application/x-www-form-urlencoded" then
|
|
|
|
-- req.ondata = function(self, chunk) ... end
|
|
|
|
-- end
|
|
|
|
-- end
|
|
|
|
end
|
|
|
|
-- setup handler of body, if any
|
|
|
|
req.ondata = function(self, chunk)
|
|
|
|
print("+B", chunk and #chunk, node.heap())
|
|
|
|
-- request ended?
|
|
|
|
if not chunk then
|
|
|
|
-- reply
|
|
|
|
--res:finish("")
|
|
|
|
res:send(nil, 200)
|
|
|
|
res:send_header("Connection", "close")
|
|
|
|
res:send("Hello, world!")
|
|
|
|
res:finish()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- or just do something not waiting till body (if any) comes
|
|
|
|
--res:finish("Hello, world!")
|
2015-01-19 21:32:16 +01:00
|
|
|
--res:finish("Salut, monde!")
|
2015-01-19 18:12:06 +01:00
|
|
|
end)
|