Hi
This could be a newbie question or I’m missing some dumb thing and I didn’t try in the lightbend forum (yet), but lets go… I’d like to implement these two routes:
/users/:userid
/users/:userid/books
This routing in expressjs is pretty much straightforward:
app.get('/user/:userid', (req, res) => {
const userid = req.params.userid;
res.send({ user: userid });
});
app.get('/user/:userid/books', (req, res) => {
const userid = req.params.userid;
res.send({ books: 36, user: userid });
})
But after searching in the docs and struggling I couldn’t do the same with the akka routing DSL. I guess my best effort is as follows:
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.directives.MethodDirectives.get
import akka.http.scaladsl.server.PathMatchers.IntNumber
import akka.util.ByteString
trait BookRoutes {
/*
/users/:userId
/users/:userId/books/
*/
lazy val bookRoutes = pathPrefix("user") {
path(IntNumber) { userId =>
get {
complete(ByteString(s"user: $userId"))
} ~
pathSuffix("books") {
get {
complete(ByteString(s"books for user $userId"))
}
}
}
}
}
This works for /user/21
but not for /user/21/books
( -> 404).
May anyone provide a hint about this can be achieved based on (well, or not )
Thanks in advance (and merry new year)