To assign a route, use the route method. You can either use a function, or add a block.
app = Navykit::new
# Using a function
app.route("METHOD", "/path/to/somewhere", some_function)
# Using a block
app.route("METHOD", "/path/to/somewhere") do |req, res|
res.send(200, "Hello")
end
For more information about route path, read Route Path.
If you want to route a GET request, you can use get instead.
app = NavyKit::new
# Using a function
app.get("/path/to/somewhere", some_function)
# Using a block
app.get(path/to/somewhere") do |req, res|
res.send(200, "Hello")
end
If you want to route a POST request, you can use post instead.
app = NavyKit::new
# Using a function
app.post("/path/to/somewhere", some_function)
# Using a block
app.get(path/to/somewhere") do |req, res|
res.send(200, "Hello")
end
By default, NavyKit sends a text/plain content when there's no route and there's no provided fallback route.
To prevent this, and to create your own fallback route, use the notfound method.
app = NavyKit::new
# Using a function
app.notfound(some_function)
# Using a block
app.notfound do |req, res|
req.send(404, "Page Not Found")
end