Left here for archival purposes.

User avatar
By Marco Schwartz
#10685 Hello,

I am trying to run a very simple web server on the ESP8266 with the latest version of the NodeMcu firmware. Here is the code:

Code: Select allsrv = net.createServer(net.TCP)

srv:listen(80, function(conn)
    conn:on("receive",function(conn, payload)

    print(payload)

    conn:send("HTTP/1.1 200 OK\r\n")
    conn:send("Connection:keep-alive\r\n")
    conn:send("Cache-Control: private, no-store\r\n\r\n")
    conn:send("<h1> Hello, NodeMcu.</h1>")
    conn:on("sent", function(conn) conn:close() end)
    end)
end)


It works once, without problems, then I need to reset the ESP8266 because it gets stuck. I tried to play with HTTP headers, same issue. Anyone know what the bug is? Thanks!
User avatar
By MK1888
#10695 It might be because you have the "sent" handler inside the "receive" handler?

Try this:
Code: Select allsrv = nil
srv = net.createServer(net.TCP)

srv:listen(80, function(conn)
    conn:on("receive",function(conn, payload)

    print(payload)

    conn:send("HTTP/1.1 200 OK\r\n")
    conn:send("Connection:keep-alive\r\n")
    conn:send("Cache-Control: private, no-store\r\n\r\n")
    conn:send("<h1> Hello, NodeMcu.</h1>")
    end)
    conn:on("sent", function(conn) conn:close() end)
end)