Correct way to provide path "request.response().sendFile(path)"

1,084 views
Skip to first unread message

Naveen

unread,
Mar 5, 2014, 8:36:26 AM3/5/14
to ve...@googlegroups.com
Hi,

I have just started to play around with vertx.
I have a java Verticle extended class in which I am doing 

                HttpServer server = vertx.createHttpServer();

server.requestHandler(new Handler<HttpServerRequest>() {
   public void handle(HttpServerRequest request) {
       logger.info("A request has arrived on the server!");
       if (request.path().equals("/")) {
        request.response().sendFile("web/index.html");
       }
   }
}).listen(8080, "localhost");

After running it, when I go to "http://localhost:8080/" in my browser, it says "Resource not found".
My directory structure is MyProject->web->index.html
But it calls the index.html correctly when I give the complete path(Eg. C:/Vertx/MyProject/web/index.html) in request.response().sendFile.

My question is, should I be giving the complete static path in "request.response().sendFile(path)". What is the correct way?

Thanks

Nick Scavelli

unread,
Mar 5, 2014, 10:36:10 AM3/5/14
to ve...@googlegroups.com
Where are you running the vertx command from ? It should work if you are in the MyProject directory.

Alexander Lehmann

unread,
Mar 5, 2014, 2:51:33 PM3/5/14
to ve...@googlegroups.com
It should work with a relative path, however you have to make sure you are in the directory that you expect.

Try adding logger.info(System.getProperty("user.dir")); to make sure

Frank Reiter

unread,
Mar 5, 2014, 5:49:42 PM3/5/14
to ve...@googlegroups.com

This sounds very much like the trouble I had a while back.  Here is a copy of my final message on the topic:

I have concluded my adventures with the web server module by specifying a fully qualified path in the config file.  I have what I need, but the web server module maintainer may want to look at this:

To summarize:

1. If I run the module from the command line, the web_root can be relative to the project root
2. If I run the module programmatically from a verticle whose working directory is the project root, the web_root is *not* relative to the project root.  I tried a few other options and never made it work.
3. If I run the module programmatically and specify a fully qualified web root, all is well.

--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Naveen

unread,
Mar 6, 2014, 1:25:24 AM3/6/14
to ve...@googlegroups.com
I am running it from MyProject directory.

Naveen

unread,
Mar 6, 2014, 1:31:42 AM3/6/14
to ve...@googlegroups.com
I added the logger, and it gives MyProject directory.

Naveen

unread,
Mar 6, 2014, 1:33:10 AM3/6/14
to ve...@googlegroups.com
Why so much complexity regarding such simple thing? Am I missing something?

Tim Fox

unread,
Mar 6, 2014, 2:51:58 AM3/6/14
to ve...@googlegroups.com
To be honest I am confused as to what the problem is. There is a send file example in the examples repo which does just about exactly as you describe and works fine for me.

Perhaps you can create an example which doesn't work as you expect, put it in a GitHub repo and then someone can take a look?

Tim Fox

unread,
Mar 6, 2014, 2:53:51 AM3/6/14
to ve...@googlegroups.com
Also.. please take a look at the example in the examples repo https://github.com/vert-x/vertx-examples/blob/master/src/raw/java/sendfile/SendFileExample.java

The README in the directory above explains how to run it.

Does it work for you?


On 06/03/14 06:33, Naveen wrote:

Alexander Lehmann

unread,
Mar 6, 2014, 6:12:45 PM3/6/14
to ve...@googlegroups.com
Works for me using exactly your example, so you are probably doing something else wrong.

You have omitted the class definition etc in your code, can you please post the complete class in a gist (or wherever) so we can compare the solution?

If the cwd is your project dir, it should work of course.

Naveen

unread,
Mar 7, 2014, 12:44:37 AM3/7/14
to ve...@googlegroups.com
I am sorry, I might have created a confusion here. I am using the gradle template(https://github.com/vert-x/vertx-gradle-template.git) for my example, and running it using "gradlew.bat runMod" (using windows).
I did create new packages. The complete directory structure is 
    MyProject---src
                 |
                 -- main
                      |
                 |    -- java 
                            |
                 |          -- com
                                |
                 |              -- myproject
                                       |
                 |                     -- MyVerticle.java
                 |
                web -- index.html
                
It works fine when I directly run "vertx run src/main/java/com/myproject/MyVerticle.java".
But it does not work when I run "gradlew.bat runMod" from MyProject. The verticle is deployed correctly, but when I point my browser to localhost:8080 it says "Resource not found".
I did put "logger.info("Curr path = " + System.getProperty("user.dir"));" in my code, and it gave me the "MyProject" path.
My mod.json has "main":"com.myproject.MyVerticle",
Now, what am I doing wrong?

And I am really sorry for creating the confusion.

Thanks

Tim Fox

unread,
Mar 7, 2014, 7:27:12 AM3/7/14
to ve...@googlegroups.com
Ok, so you're running your code in a module, that's different to running it as a raw verticle.

When running in a module, Vert.x sees "." as the directory at the top of the module.

When using the Gradle template or Maven archetype (which both use the standard Maven project layout), any resources in src/main/resources will go into the top of your module, so if you want to serve your index.html from your module you should put it in their in your project layout.

http://vertx.io/mods_manual.html#preserve-cwd
For more options, visit https://groups.google.com/d/optout.

Naveen

unread,
Mar 7, 2014, 8:38:05 AM3/7/14
to ve...@googlegroups.com
It works fine now.

Thanks Tim
Reply all
Reply to author
Forward
0 new messages