Skip to content

Commit 67a637e

Browse files
authored
Merge pull request #34278 from gsmet/static-files
Describe how you can serve static files with a Vert.x route
2 parents 3a60357 + 7981e4f commit 67a637e

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

docs/src/main/asciidoc/http-reference.adoc

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ Servlet filter, otherwise it will run directly on top of Vert.x with no Servlet
2424

2525
== Serving Static Resources
2626

27-
To serve static resources you must place them in the `META-INF/resources` directory of your application. This location
27+
=== From the application jar
28+
29+
To serve static resources from the application jar, you must place them in the `META-INF/resources` directory of your application. This location
2830
was chosen as it is the standard location for resources in `jar` files as defined by the Servlet spec. Even though
2931
Quarkus can be used without Servlet, following this convention allows existing code that places its resources in this
3032
location to function correctly.
3133

32-
=== WebJar Locator Support
34+
=== From WebJars
3335

3436
If you are using webjars, like the following JQuery one:
3537

@@ -68,6 +70,33 @@ To use it, add the following to your project's dependencies:
6870
implementation("io.quarkus:quarkus-webjars-locator")
6971
----
7072

73+
=== From a local directory
74+
75+
Static resources can be served from a local directory by installing an additional route in the Vert.x router.
76+
77+
For instance, to serve resources from the `static/` directory relative to the current path at http://localhost:8080/static/,
78+
you can install the following route:
79+
80+
[source,java]
81+
----
82+
package org.acme;
83+
84+
import io.quarkus.runtime.StartupEvent;
85+
import io.vertx.ext.web.Router;
86+
import io.vertx.ext.web.handler.StaticHandler;
87+
import jakarta.enterprise.event.Observes;
88+
89+
90+
public class StaticResources {
91+
92+
void installRoute(@Observes StartupEvent startupEvent, Router router) {
93+
router.route()
94+
.path("/static/*")
95+
.handler(StaticHandler.create("static/"));
96+
}
97+
}
98+
----
99+
71100
=== HTTP Compression
72101

73102
The response body of a static resource is not compressed by default.

0 commit comments

Comments
 (0)