Created
March 1, 2015 09:52
-
-
Save leolux/eb77b3451e598f22a449 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
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
The output is:
"/
Route not found"