Sub-routers

有时候你也许会有很多handler来处理请求, 在这种情况下将这些handler分配到多个route中是非常有好处的. 另外如果你想在不同的应用程序中通过不同的root路径来复用这些handler, 这种做法同样是很有帮助的.

为了能达到这种效果, 你可以

Sometimes if you have a lot of handlers it can make sense to split them up into multiple routers. This is also useful if you want to reuse a set of handlers in a different application, rooted at a different path root.

To do this you can mount a router at a mount point in another router. The router that is mounted is called a sub-router. Sub routers can mount other sub routers so you can have several levels of sub-routers if you like.

Let’s look at a simple example of a sub-router mounted with another router.

This sub-router will maintain the set of handlers that corresponds to a simple fictional REST API. We will mount that on another router. The full implementation of the REST API is not shown.

Here’s the sub-router:

  1. Router restAPI = Router.router(vertx);
  2. restAPI.get("/products/:productID").handler(rc -> {
  3. // TODO Handle the lookup of the product....
  4. rc.response().write(productJSON);
  5. });
  6. restAPI.put("/products/:productID").handler(rc -> {
  7. // TODO Add a new product...
  8. rc.response().end();
  9. });
  10. restAPI.delete("/products/:productID").handler(rc -> {
  11. // TODO delete the product...
  12. rc.response().end();
  13. });

If this router was used as a top level router, then GET/PUT/DELETE requests to urls like /products/product1234 would invoke the API.

However, let’s say we already have a web-site as described by another router:

  1. Router mainRouter = Router.router(vertx);
  2. // Handle static resources
  3. mainRouter.route("/static/*").handler(myStaticHandler);
  4. mainRouter.route(".*\\.templ").handler(myTemplateHandler);

We can now mount the sub router on the main router, against a mount point, in this case /productsAPI

mainRouter.mountSubRouter(“/productsAPI”, restAPI); This means the REST API is now accessible via paths like: /productsAPI/products/product1234