Skip to content

Instantly share code, notes, and snippets.

@leolux
Created March 1, 2015 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leolux/eb77b3451e598f22a449 to your computer and use it in GitHub Desktop.
Save leolux/eb77b3451e598f22a449 to your computer and use it in GitHub Desktop.
package reproducer.apex.hashtag;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.apex.core.Router;
import java.net.HttpURLConnection;
/**
* <ol>
* <li>Listen on GET request on port 99</li>
* <li>Redirect client to localhost:8080/#/somepath/abcde</li>
* <li>Test if apex route has been found</li>
* </ol>
*/
public class Start {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
startServerOnPort99ToRedirectToPort8080(vertx);
startServerOnPort8080(vertx);
}
private static void startServerOnPort99ToRedirectToPort8080(Vertx vertx) {
HttpServerOptions options = new HttpServerOptions();
options.setPort(99);
HttpServer server = vertx.createHttpServer(options);
Router router = Router.router(vertx);
router.routeWithRegex(".*").handler(
routingContext -> {
HttpServerResponse response = routingContext.response();
response.setStatusCode(HttpURLConnection.HTTP_MOVED_PERM);
response.putHeader("Location",
"http://localhost:8080/#/somepath/abcd");
response.putHeader("Content-Length", String.valueOf(0));
response.end();
});
server.requestHandler(router::accept).listen();
}
private static void startServerOnPort8080(Vertx vertx) {
HttpServerOptions options = new HttpServerOptions();
options.setPort(8080);
HttpServer server = vertx.createHttpServer(options);
Router router = Router.router(vertx);
router.route().method(HttpMethod.GET).path("/#/somepath/:param1")
.handler(routingContext -> {
System.out.println(routingContext.request().path());
System.out.println("Route found");
routingContext.response().setStatusCode(200).end();
});
router.routeWithRegex(".*").handler(routingContext -> {
System.out.println(routingContext.request().path());
System.out.println("Route not found");
routingContext.response().setStatusCode(404).end();
});
server.requestHandler(router::accept).listen();
}
}
@leolux
Copy link
Author

leolux commented Mar 1, 2015

The output is:
"/
Route not found"

@leolux
Copy link
Author

leolux commented Mar 1, 2015

Everything works as expected because browsers do not transmit the #-part to the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment