In vert.x web studying the root, I came across this code:

HttpServer server = Vertx.vertx().createHttpServer(); Router router = Router.router(Vertx.vertx()); Route route1 = router.route("/some/path/").handler(routingContext -> { HttpServerResponse response = routingContext.response(); response.setChunked(true); response.write("route1\n"); routingContext.vertx().setTimer(5000, tid -> routingContext.next()); }); Route route2 = router.route("/some/path/").handler(routingContext -> { HttpServerResponse response = routingContext.response(); response.write("route2\n"); routingContext.vertx().setTimer(5000, tid -> routingContext.next()); }); Route route3 = router.route("/some/path/").handler(routingContext -> { HttpServerResponse response = routingContext.response(); response.write("route3"); routingContext.response().end(); }); server.requestHandler(router::accept).listen(8080, new Handler<AsyncResult<HttpServer>>() { @Override public void handle(AsyncResult<HttpServer> event) { if(event.succeeded()) System.out.println("server started"); else System.out.println("server error"); } }); 

I can not understand what the route ("/ some / path /") method is responsible for. The method description says:

 Route route(String path); Add a route that matches the specified HTTP method and path regex @param method the HTTP method to match @param regex URI paths that begin with a match for this regex will match @return the route 

The method accepts a string (I do not understand what this string is, what is written in it). Returns an instance of the Route interface. Got into it:

 public interface Route { Add an HTTP method for this route. By default a route will match all HTTP methods. If any are specified then the route will only match any of the specified methods @param method the HTTP method to add @return a reference to this, so the API can be used fluently 

It is not clear from this alas

  • Router is a router. Route - route (or path). Router decides which path to send the incoming request to. Each route is registered in the router with an indication of what requests to send on it. Router looks at the request, looks at what routes it has for this request and sends it there - Sergey
  • Roughly understood. In this code, an Http connection. where do these 3 routers go? - Iga
  • one
    Router one. And 3 - this rue, (routes, paths). They all lead each to their handler handler. The handler does something there on this request, writes something in response (response) and can complete further ordeals of the request or send by the following route, which accepts the request - Sergey
  • so I could write down the handlers for each route. then create routes and each poke the appropriate handler? and depending on the specified path, client requests will be sent to these handlers? - Iga
  • @lga yes. (so that the message does not look short) - Sergey

1 answer 1

So, having understood a bit, I will try to answer this question myself (who knows, who can come in handy). In this case, we have a router that pulls out 3 routes for the connected client. each route has its own handler, which is activated when the client goes to the appropriate address. for example, we have 3 possible routes on the router (for example, let's take a website):

 Route route1 =router.route("/registration/") Route route2 = router.route("/gallery/") Route route3 = router.route("/video/") 

and in the code below, in front of listen() we have such a requestHandler(router::accept) (as I understand it, roughly speaking, installing the router on the server). So, the client indicates the address mysite.com/registration. the server accepts this case and looks to see if there is such a route. if it is, then the handler is activated, which gives a signal to the corresponding request processing (the handler can already implement the user registration mechanism). in case of a request with other ways, the client will go to the gallery and video archive. in the end, the route("/video/") method route("/video/") takes the path we want to write in the root. something like this. a little clumsy, but I think I caught the essence ...