tarantool/http

httpd:route doesn't work

cptLamo opened this issue · 4 comments

Hello, i try to run http server on tarantool, but when i need to run route, it return me error.

  • error: '[string "return httpd:route( { path = ''/metrics'' }, pr..."]:1: attempt
    to call method ''route'' (a nil value)

Any advice?
Thank you.

Hello, thank you for a question I assume that it's a quite frequent question

It was changed in v2+

local httpd = http_server.new(HOST, PORT)
local router = http_router.new()
     :route({ path = '/format', method = 'GET' }, handler)
httpd:set_router(router)
httpd:start()

Use fixed old version (especially not scm) or update your code

D-Y-V commented

This work.
Create module /usr/local/share/tarantool/site.lua
----------------------------BOF

local site = {}

--local function private()
--    print("in private function")
--end

function site.index(req)
    return {
        status = 200,
        headers = { ['content-type'] = 'text/html; charset=utf8' },
        body = [[
            <html>
                <body>Hello, world!</body>
            </html>
        ]]
    }
end

--function site.bar()
--    private()
--   site.foo() -- need to prefix function call with module
--end

return site

-----------------------------EOF

Connect to server. In console do:

srv1 = require('http.server').new('0.0.0.0', 8080)
router = require('http.router').new({charset = "utf8"})
srv1:set_router(router)
router:route({
    path = '/',
    name = 'index',
    method = 'GET',
  }, 'site#index')
srv1:start()
D-Y-V commented

We discarded http v2. See details in #134 and https://github.com/tarantool/http/releases/tag/1.1.1.