vertx-web: where do I put webroot folder?

1,410 views
Skip to first unread message

Dave Taubler

unread,
Aug 2, 2015, 11:30:40 PM8/2/15
to vert.x
This should be an easy one question; it just hasn't been for me so far. 

I've been working with vert.x 2 for a bit and switched to vert.x 3 recently. I'm pretty excited about it in general. So I thought I'd try a simple vertx-web example but can't get past a simple serving up of static files.

My server class contains the following snippets:

 HttpServer server = vertx.createHttpServer();
 
Router router = ...;
 router
.route("/static/*").handler(StaticHandler.create().setCachingEnabled(false));
 server
.requestHandler(router::accept).listen(ctx.port);


I'm using Eclipse, but have also been trying running vertx from the the command line. I'm also using Maven. I have three webroot folders, and vert.x can find none of them:

    myproject/webroot
    myproject
/src/main/resources/webroot
    myproject
/src/main/java/webroot


Each of those 'webroot's contains an index.html, and a css/base.css file.

The first one is in my project's root folder. The second is in the Maven resources folder, and the third should be flat-out on my `classpath`. In my Eclipse run config, I added myproject/src/main/resources/webroot to the classpath, and I made sure my working directory was set to 'myproject'. When running from the command line, I'm in the myproject directory, and my script looks like this:

JAVA_OPTS="-Dmyproject.port=8099" CLASSPATH="src/main/java:src/main/resources:target/dependencies/*:target/classes" vertx run com.my.MyProject


No matter what, I always get 404s when I try any of these URLs:

    http://localhost:8099
    http
://localhost:8099/
    http
://localhost:8099/index.html
    http
://localhost:8099/static/css/base.css


Anything else I need to be doing?

Dave Taubler

unread,
Aug 3, 2015, 10:37:44 PM8/3/15
to vert.x
Alright, I'll somewhat answer my own question. First, I should point out that I was keying off of the static-content parts of the vertx-web docs (http://vertx.io/docs/vertx-web/js/#_serving_static_resources):

Any requests to paths handled by the static handler will result in files being served from a directory on the file system or from the classpath. The default static file directory is webroot but this can be configured.

In the following example all requests to paths starting with /static/ will get served from the directory webroot:

var StaticHandler = require("vertx-web-js/static_handler");

router.route("/static/*").handler(StaticHandler.create().handle);
and

Any requests to the root path / will cause the index page to be served. By default the index page is index.html. This can be configured with setIndexPage.

It seemed to me that if I didn't explicitly define a handler for "/", then index.html would implicitly be served. And it also seemed that just creating a StaticHandler and adding it to the router would suffice for CSS/JS/IMG resources. I think my assumptions were incorrect.

So I added the following, which seem to be what was needed:

first, I told the StaticHandler explicitly to look for a "webroot" folder:

router.route("/static/*").handler(StaticHandler.create("webroot").setCachingEnabled(false));


then, I explicitly added a route to my router to handle requests to "/":


                router.route("/").handler(ctx -> {

                        Logger.log("Got an HTTP request to /");

                        ctx.response().sendFile("webroot/index.html").end();

                });



I still don't think that matches with what the docs suggest, but that got it to work.

Tim Fox

unread,
Aug 10, 2015, 5:58:25 AM8/10/15
to ve...@googlegroups.com
Take a look at the vertx-examples repo. There are static examples there that should "just work". You can copy them into your own project.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/63fd197d-5bcc-46e8-bbc1-c181d67d47ce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages