npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

create-resty-app

v0.7.0

Published

high performance router

Downloads

232

Readme

lua-resty-router

Elegant, performant and productive router for Openresty.

Quick Start

It's recommended to use npm to scaffold a lua-resty-router project.

npm create resty-app@latest -y

Then follow the instructions to complete the project.

Dependencies

Synopsis

local Router = require('resty.router')

local router = Router:new()

local cnt = 0
local error_cnt = 0

-- Test events
-- for test purpose, assume the nginx is single-threaded
router:on('add', function(ctx)
  cnt = cnt + 1
end)

router:on('error', function(ctx)
  error_cnt = error_cnt + 1
end)

-- Test plugins
router:use(function(ctx)
  ctx.cnt = cnt
  ctx.error_cnt = error_cnt
end)

-- 1. Test static path
router:get("/hello", function()
  return "Hello World"
end)

-- Test custom success status code
router:get("/hello-201", function()
  return "Hello World", 201
end)

-- 2. Test JSON response
router:get("/json", function()
  return { message = "success", code = 0 }
end)

-- 3. Test dynamic path parameters
router:get("/users/#id", function(ctx)
  return {
    id = ctx.params.id,
    type = type(ctx.params.id)
  }
end)

router:get("/users/:name", function(ctx)
  return {
    name = ctx.params.name,
    type = type(ctx.params.name)
  }
end)

-- 4. Test regex path
router:get([[/version/<ver>\d+\.\d+]], function(ctx)
  return {
    version = ctx.params.ver
  }
end)

-- 5. Test wildcard
router:get("/files/*path", function(ctx)
  return ctx.params.path
end)

-- 6. Test other HTTP methods
router:post("/accounts", function(ctx)
  return { method = "POST" }
end)

router:put("/accounts/#id", function(ctx)
  return { method = "PUT", id = ctx.params.id }
end)

-- 7. Test error handling
router:get("/error", function()
  error("Test Error")
end)

-- Test custom error thrown by error()
router:get("/custom-error", function()
  error({ code = 400, message = "Custom Error" })
end)

-- Test error in the form of return nil, err, code
router:get("/return-error", function()
  return nil, "Parameter Error", 402
end)

-- Test handled error
router:get("/handled-error", function()
  error { "handled error" }
end)

-- 8. Test status code
router:get("/404", function()
  return nil, "Not Found", 404
end)

-- 9. Test HTML response
router:get("/html", function()
  return "<h1>Hello HTML</h1>"
end)
-- Test HTML error
router:get("/html-error", function()
  return nil, "<h1>Hello HTML error</h1>", 501
end)
-- Test HTML error thrown by error()
router:get("/html-error2", function()
  error { "<h1>Hello HTML error2</h1>" }
end)

-- 10. Test function return
router:get("/func", function(ctx)
  return function()
    ngx.header.content_type = 'text/plain; charset=utf-8'
    ngx.say("function called")
  end
end)

-- 11. Check if events are executing properly
router:get("/add", function(ctx)
  router:emit('add')
  return ctx.cnt
end)

router:get("/events", function(ctx)
  return ctx.cnt
end)

return router

Api

new

(method) Router:new()
  -> Router

create a router.

insert

(method) Router:insert(path: string, handler: string|function, methods?: string|string[])
  -> Router

insert a route.

extend

---@alias Route {[1]:string, [2]:function|string, [3]?:string|string[]}
(method) Router:extend(routes: Route[])
  -> Router

insert routes to a router.

match

(method) Router:match(path: string, method: string)
  ->
  1. string|function
  2. { [string]: string|number }?

match a http request

run

(method) Router:run()
  -> nil

dispatch http request

fs

(method) Router:fs(dir: string)
  -> nil

load routes from directory (requires lfs or syscall.lfs module).

reference