Basics

In post about middlewares we said that we can define middlewares on global scope, route scope, and on scope of some set of routes. Here we will explain how we can define middlewares on set of routes.

Neo sees set of routes as regions. Regions can define HTTP methods and middlewares.

You can create new region by calling Region fn.

region := app.Region()

Now you can define routes and middlewares on region object.

region.Get("/path", func(ctx *neo.Ctx) (int, error) {
    // route handler implementation
})

region.Use(func(ctx *neo.Ctx, next neo.Next) {
    // middleware implementation
})

You can define multiple regions per application. So you can have:

authenticated := app.Region()
authenticated.Use(authenticate)

unauthenticated := app.Region()
// etc.

So using regions it will be really simple to separate routes which requires authentication for example.

Region With Default Prefix

You have an option to specify URL prefix for routes inside same region.

With this feature if you a have number of routes starting with the same URL prefix, you can group them into region. You will create region, set common prefix, and omit prefix when specifying child route URLs.

Example:

api := app.Region().Prefix("/my/prefix")

// it will be invoked on GET /my/prefix/users
api.Get("/users", func(ctx *neo.Ctx) (int, error) {
})