Skip to main content
version 1.0.0

Routing

Basic routing

The most basic Athenna routes accept an url and a closure, providing a very simple and expressive method of defining routes and behavior without complicated routing configuration files:

import { Route } from '@athenna/http' // Route Facade

Route.get('/welcome', ({ response }) => {
return response.status(200).send({ hello: 'world' })
})

Available Router Methods

The router allows you to register routes that respond to any HTTP verb:

Route.get(url, callback)
Route.post(url, callback)
Route.put(url, callback)
Route.patch(url, callback)
Route.delete(url, callback)
Route.options(url, callback)

Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the any method to respond to all HTTP verbs:

Route.any('/', () => {
//
})
tip

When defining multiple routes that share the same url, routes using the get, post, put, patch, delete, and options methods should be defined before routes using the any and redirect methods. This ensures the incoming request is matched with the correct route.

Redirect routes

If you are defining a route that redirects to another url, you may use the Route.redirect method. This method provides a convenient shortcut so that you do not have to define a full route or controller for performing a simple redirect:

Route.redirect('/here', '/there')

By default, Route.redirect returns a 302 status code. You may customize the status code using the optional third parameter:

Route.redirect('/here', '/there', 301)

The route list

The route:list command can easily provide an overview of all the routes that are defined by your application:

node artisan route:list

By default, the route middleware that are assigned to each route will not be displayed in the route:list output; however, you can instruct Athenna to display the route middleware by adding the -m, --middleware option to the command:

node artisan route:list --middleware

Route parameters

Sometimes you will need to capture segments of the URL within your route. For example, you may need to capture a user's ID from the URL. You may do so by defining route parameters:

Route.get('/user/:id', ({ response, params }) => {
return response.send({ user: `User ${params.id}` })
})

You may define as many route parameters as required by your route:

Route.get('/posts/:postId/comments/:commentId', ({ response, params }) => {
return response.send({ params })
})

Route groups

Route groups allow you to share route attributes, such as middleware, across a large number of routes without needing to define those attributes on each individual route.

Middleware

To assign middlewares to all routes within a group, you may use the middleware method before defining the group. Middleware are executed in the order they are listed in the array:

Route.group(() => {
Route.get('/', () => {
// Uses first & second middleware...
})

Route.get('/user/profile', () => {
// Uses first & second middleware...
})
}).middleware(['first', 'second'])

Route prefixes

The prefix method may be used to prefix each route in the group with a given URL. For example, you may want to prefix all route URLs within the group with admin:

Route.group(() => {
Route.get('/users', () => {
// Matches only the "/admin/users" URL
})
}).prefix('admin')

Rate limiting

Rate limiters ensure the fair usage of your http server by clients. It regulates the number of times a user can request your application in a given time-frame.

Athenna uses the fastify-rate-limit plugin inside HttpKernel. All the configurations that fastify-rate-limit supports can be set inside config/http.js file in the rateLimit object.

Rate limit plugin is registered in your http application by default, but you can remove it setting the noRateLimit as true in config/http.js file.

tip

For more information on Rate limit and Rate limit headers, please consult the Meta article about rate limiters.

Cross-origin resource sharing (CORS)

Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.

Athenna uses the fastify-cors plugin inside HttpKernel. All the configurations that fastify-cors supports can be set inside config/http.js file in the cors object.

Rate limit plugin is registered in your http application by default, but you can remove it setting the noCors as true in config/http.js file.

tip

For more information on CORS and CORS headers, please consult the MDN web documentation on CORS.