Skip to content

How can I use Luv to implement an HTTP server? #761

Open
@z1485671578

Description

@z1485671578

I followed the instructions to use the return require "luv" to create a new uv.lua file, and then introduced luvit deps file, but I don't have luvi, so I don't know how to use luv to create an HTTP server.
the lua file content is:

local http = require("http")

local function onRequest(req, res)
  local body = "Hello world\n"
  res:setHeader("Content-Type", "text/plain")
  res:setHeader("Content-Length", #body)
  res:finish(body)
end

http.createServer(onRequest):listen(8080)
print("Server listening at http://localhost:8080/")

uv.run()

the error message is as follows:

Server listening at http://localhost:8080/
Uncaught Error: ...\MotionMaster\x64\Release\lua\stream\stream_readable.lua:810: attempt to index a nil value (global 'process')
stack traceback:
        ...\MotionMaster\x64\Release\lua\stream\stream_readable.lua:810: in upvalue 'resume'
        ...\MotionMaster\x64\Release\lua\stream\stream_readable.lua:802: in field 'resume'
        D:\Source\Repos\MotionMaster\x64\Release\lua\net.lua:183: in method 'resume'
        ...\MotionMaster\x64\Release\lua\stream\stream_readable.lua:767: in method 'on'
        D:\Source\Repos\MotionMaster\x64\Release\lua\http.lua:290: in function 'http.handleConnection'
        (...tail calls...)
        D:\Source\Repos\MotionMaster\x64\Release\lua\core.lua:248: in method 'emit'
        D:\Source\Repos\MotionMaster\x64\Release\lua\net.lua:331: in local 'handler'
        D:\Source\Repos\MotionMaster\x64\Release\lua\core.lua:248: in method 'emit'
        D:\Source\Repos\MotionMaster\x64\Release\lua\net.lua:280: in function <D:\Source\Repos\MotionMaster\x64\Release\lua\net.lua:276>
        [C]: in function 'luv.run'
        .../MotionMaster/x64/Release/Example/luv httpserver测试.lua:14: in main chunk

How can I fix it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions