Basics

Every time when user makes request to your Web Application, Neo will try to match one of declared routes. If none of them matches, it will return 404.

Neo has simple but powerful API for defining routes. When you want to declare route you have to call one of functions which corresponds to one of supported HTTP methods. Let’s define some usual GET, POST, PUT and DELETE routes.

app.Get("/article", func(ctx *neo.Ctx) (int, error) {
    // implementation
})

app.Post("/user", func(ctx *neo.Ctx) (int, error) {
    // implementation
})

app.Put("/image/:id", func(ctx *neo.Ctx) (int, error) {
    // implementation
})

app.Delete("/user/:id", func(ctx *neo.Ctx) (int, error) {
    // implementation
})

Every route handler should be function with func(*neo.Ctx) (int, error) signature. If error is returned from route handler, neo will return 500 to the client, and log error.

Route parameters

In previous example for PUT route we saw "/image/:id" route path definition. Interesting part is “:id” part. This is route parameter, it is variabile part of route which can change, and you can easily get it’s value.

app.Put("/image/:id", func(ctx *neo.Ctx) (int, error) {
    fmt.Println("Updating image with id " + ctx.Req.Params.Get("id"))
    // rest...
})

In case that we have something like /user/:userid/image/:imagename you would get those parameters by using:

ctx.Req.Params.Get("userid")
// and (`Params` is actually map, so you can access it without `Get`)
ctx.Req.Params["imagename"]

Wildcards

Neo router has support for wildcards. One of usecases for this is CORS. If you want to match all OPTIONS requests you can do something like this:

app.Options("*", func(ctx *neo.Ctx) (int, error) {
    // do something
})

Named wildcards

app.Options("/some/*key", func(ctx *neo.Ctx) (int, error) {
    key := ctx.Req.Params["key"]
    // do something
})

But you can also mix route parameters and wildcards, so this is also possible:

app.Options("/some/*/:param/bla", func(ctx *neo.Ctx) (int, error) {
    // will match "/some/fsfg/123/bla", "/some/ghf/456/bla", etc.
})