This should be an easy one, but it's not been so far. I've been working with vert.x 2
for a bit and switched to vert.x 3
recently. I thought I'd try a simple vertx-web example but can't get past a simple serving up of static files.
这应该是一个简单的方法,但目前还没有。我一直和维特一起工作。然后切换到vert。最近x 3。我想尝试一个简单的vertx-web示例,但是无法通过一个简单的静态文件服务。
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:
我正在使用Eclipse,但也在尝试从命令行运行vertx。我也使用Maven。我有三个webroot文件夹和vert。x找不到它们:
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.
每个webroot's都包含一个索引。html和css /基地。css文件。
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:
第一个在我的项目的根文件夹中。第二个在Maven资源文件夹中,第三个应该放在我的类路径中。在我的Eclipse运行配置中,我将myproject/src/main/resources/webroot添加到类路径中,并确保我的工作目录设置为“myproject”。当从命令行运行时,我在myproject目录中,我的脚本如下所示:
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:
无论如何,当我尝试这些url时,我总是得到404s:
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?
我还需要做什么吗?
8 个解决方案
#1
7
The solution I found depends on the answer to the questions: where is the static content going to be at runtime, and from where would you like vertx to serve it?
我找到的解决方案取决于问题的答案:静态内容在运行时将位于何处,您希望vertx从何处提供这些内容?
In my case, the installed file system would be where the static content was located (not in a jar file), and I wanted vertx to serve it from that location so that it could be updated live. So, I disabled classpath resolving in the StaticHandler by setting the JVM system property vertx.disableFileCPResolving
to true
at vertx startup.
在我的例子中,已安装的文件系统将是静态内容所在的位置(不是在jar文件中),我希望vertx从该位置服务它,以便它能够被更新。因此,我在StaticHandler中通过设置JVM system属性vertx来禁用类路径解析。在vertx启动时,disablefilecolving to true。
Then I placed the webroot
folder under the directory from which the jvm is started. In my case, I'm using scripts that guarantee the jvm's cwd
is always <app-root>/bin
, so dropping the content in <app-root>/bin/webroot
was sufficient. If you can't make guarantees about where the jvm will be started from, it might be tougher, because you may need to pass an absolute path to StaticHandler.webroot() pointing to this fixed location, but I think there is an open issue regarding support for this (see here).
然后,我将webroot文件夹放置在启动jvm的目录下。在我的例子中,我使用的脚本保证jvm的cwd始终
If the static content is going to be packaged into a jar, it's a little simpler. You can add 'webroot' as a resource in the jar and place all the content of interest in there. In this case you don't want to disable classpath resolving, so either set vertx.disableFileCPResolving
to false
, or don't set it at all. Now when you run vertx
, it will find webroot
in the jar file and extract its contents to <cwd>/.vertx/.file-cache-<guid>
(where cwd
is wherever you started the jvm from), and serve the contents from there. Note that this isn't viable if you intend to be able to do live updates to the content, because any changes to the files under that directory will get lost when vertx shuts down, because vertx-web
will delete that directory. On restart, it will retrieve the original files from the jar resource again, and changes will be lost.
如果静态内容将被打包到一个jar中,那么它会更简单一些。您可以在jar中添加“webroot”作为资源,并将感兴趣的所有内容放在其中。在这种情况下,您不希望禁用类路径解析,因此可以设置vertx。disableFileCPResolving to false,或者完全不设置它。现在,当您运行vertx时,它将在jar文件中找到webroot并将其内容提取到< c> /.vertx/。文件- wd -
Anyway, hope this helps.
无论如何,希望这有助于。
#2
4
The default classpath setting worked for me. I used the following hierarchy:
默认的类路径设置对我有效。我使用了以下层次结构:
Keep the static file in main/resources/webroot
directory
将静态文件保存在main/resources/webroot目录中
<AppRoot>/src/main/resources/webroot/static/index.html
And initialize vertx as follows:
并初始化vertx如下:
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
Vertx vertx = Vertx.vertx();
Router router = Router.router(vertx);
router.route().handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
Now you can access it from this URL: http://localhost:8080/static/
现在可以从这个URL访问它:http://localhost:8080/static/。
Refer vertx-web documentation for more info.
更多信息请参考vertx-web文档。
#3
4
Execute the following code at any breakpoint and it'll tell you the root directory from which it will resolve your resource request:
在任何断点处执行以下代码,它将告诉您解决资源请求的根目录:
new File("").getCanonicalPath()
Request: localhost:8080/static/script.js
要求:localhost:8080 /静态/ script.js
Handler:
处理程序:
router.route("/static/*").handler(StaticHandler.create("webroot"));
The file should be in MODULE_ROOT/webroot/script.js
文件应该在MODULE_ROOT/webroot/script.js中
Debug all the way into the StaticHandlerImpl#sendStatic
method to see how your files are being resolved, and use the debug expression window liberally to interrogate the file system.
一直调试到StaticHandlerImpl#sendStatic方法,查看如何解析文件,并使用Debug expression窗口检查文件系统。
#4
2
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):
好的,我会回答我自己的问题。首先,我要指出的是,我已经关闭了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.
任何由静态处理程序处理的路径的请求都会导致文件系统或类路径上的文件被服务。默认的静态文件目录是webroot,但是可以配置它。
In the following example all requests to paths starting with /static/ will get served from the directory webroot:
在下面的示例中,所有以/static/开头的路径请求都将从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.
任何对根路径的请求都会导致索引页被服务。默认情况下,索引页面是index.html。这可以配置为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.
在我看来,如果我没有显式地为“/”定义一个处理程序,那么索引。html将隐式地提供服务。而且似乎仅仅创建一个静态服务器并将其添加到路由器就足够使用CSS/JS/IMG资源了。我认为我的假设是错误的。
So I added the following, which seem to be what was needed:
所以我补充了以下内容,这似乎是我们所需要的:
first, I told the StaticHandler explicitly to look for a "webroot" folder:
首先,我明确地告诉StaticHandler寻找一个“webroot”文件夹:
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();
});
#5
2
this solution worked for me, to add more details here is the folder structure for me
这个解决方案对我来说是有效的,在这里添加更多的细节是我的文件夹结构
src
main
java
com
-- java files with vertx
resources
webroot
html
js
#6
1
Static files kept in /src/main/resources/assets/
保存在/src/main/resources/assets/中的静态文件
ex: index.html
例:index . html
Router router = Router.router(vertx);
router.route("/files/*").handler(StaticHandler.create("assets"));
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
Now you can access it from http://localhost:8080/files/index.html
现在可以通过http://localhost:8080/files/index.html访问它
If you look at router.route("/files/*").handler(StaticHandler.create("assets"));
如果您查看route(“/files/*”).handler(StaticHandler.create(“assets”);
When a request matches /files/
it calls the StaticHandler which specifies assets
folder as the web-root
当请求匹配/文件/它调用StaticHandler时,它指定assets文件夹为web根目录
#7
1
Directory
目录
<AppRoot>/webroot/index.html
Vertx
Vertx
router.route("/app/*").handler(StaticHandler.create("webroot").setCachingEnabled(false));
URL
URL
http://127.0.0.1/app/index.html
Add the following Java VM options to enable hot reload
添加以下Java VM选项以启用热重载
-Dvertx.disableFileCPResolving=true
#8
0
I believe people are getting tripped up by the examples in the documentation for serving static resources at http://vertx.io/docs/vertx-web/kotlin/#_serving_static_resources -- some of the answers even go so far as to change the route path, which is not what is desired!
我相信在http://vertx上为静态资源提供服务的文档中的例子会让人们犯错误。io/docs/vertx-web/kotlin/#_serving_static_resources——有些答案甚至会更改路由路径,这不是我们想要的!
I have raised https://github.com/vert-x3/issues/issues/265 with details.
我已经提出了https://github.com/vert-x3/issues/issues/ issues/265详细信息。
Among others, OP tried:
其中,OP尝试:
http://localhost:8099/static/css/base.css
That should have worked, the rest of the paths were not valid, and src/main/resources/webroot
is a good place for packaged-up static resources.
这应该有效,其余路径无效,src/main/resources/webroot是打包静态资源的好地方。
#1
7
The solution I found depends on the answer to the questions: where is the static content going to be at runtime, and from where would you like vertx to serve it?
我找到的解决方案取决于问题的答案:静态内容在运行时将位于何处,您希望vertx从何处提供这些内容?
In my case, the installed file system would be where the static content was located (not in a jar file), and I wanted vertx to serve it from that location so that it could be updated live. So, I disabled classpath resolving in the StaticHandler by setting the JVM system property vertx.disableFileCPResolving
to true
at vertx startup.
在我的例子中,已安装的文件系统将是静态内容所在的位置(不是在jar文件中),我希望vertx从该位置服务它,以便它能够被更新。因此,我在StaticHandler中通过设置JVM system属性vertx来禁用类路径解析。在vertx启动时,disablefilecolving to true。
Then I placed the webroot
folder under the directory from which the jvm is started. In my case, I'm using scripts that guarantee the jvm's cwd
is always <app-root>/bin
, so dropping the content in <app-root>/bin/webroot
was sufficient. If you can't make guarantees about where the jvm will be started from, it might be tougher, because you may need to pass an absolute path to StaticHandler.webroot() pointing to this fixed location, but I think there is an open issue regarding support for this (see here).
然后,我将webroot文件夹放置在启动jvm的目录下。在我的例子中,我使用的脚本保证jvm的cwd始终
If the static content is going to be packaged into a jar, it's a little simpler. You can add 'webroot' as a resource in the jar and place all the content of interest in there. In this case you don't want to disable classpath resolving, so either set vertx.disableFileCPResolving
to false
, or don't set it at all. Now when you run vertx
, it will find webroot
in the jar file and extract its contents to <cwd>/.vertx/.file-cache-<guid>
(where cwd
is wherever you started the jvm from), and serve the contents from there. Note that this isn't viable if you intend to be able to do live updates to the content, because any changes to the files under that directory will get lost when vertx shuts down, because vertx-web
will delete that directory. On restart, it will retrieve the original files from the jar resource again, and changes will be lost.
如果静态内容将被打包到一个jar中,那么它会更简单一些。您可以在jar中添加“webroot”作为资源,并将感兴趣的所有内容放在其中。在这种情况下,您不希望禁用类路径解析,因此可以设置vertx。disableFileCPResolving to false,或者完全不设置它。现在,当您运行vertx时,它将在jar文件中找到webroot并将其内容提取到< c> /.vertx/。文件- wd -
Anyway, hope this helps.
无论如何,希望这有助于。
#2
4
The default classpath setting worked for me. I used the following hierarchy:
默认的类路径设置对我有效。我使用了以下层次结构:
Keep the static file in main/resources/webroot
directory
将静态文件保存在main/resources/webroot目录中
<AppRoot>/src/main/resources/webroot/static/index.html
And initialize vertx as follows:
并初始化vertx如下:
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
Vertx vertx = Vertx.vertx();
Router router = Router.router(vertx);
router.route().handler(StaticHandler.create());
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
Now you can access it from this URL: http://localhost:8080/static/
现在可以从这个URL访问它:http://localhost:8080/static/。
Refer vertx-web documentation for more info.
更多信息请参考vertx-web文档。
#3
4
Execute the following code at any breakpoint and it'll tell you the root directory from which it will resolve your resource request:
在任何断点处执行以下代码,它将告诉您解决资源请求的根目录:
new File("").getCanonicalPath()
Request: localhost:8080/static/script.js
要求:localhost:8080 /静态/ script.js
Handler:
处理程序:
router.route("/static/*").handler(StaticHandler.create("webroot"));
The file should be in MODULE_ROOT/webroot/script.js
文件应该在MODULE_ROOT/webroot/script.js中
Debug all the way into the StaticHandlerImpl#sendStatic
method to see how your files are being resolved, and use the debug expression window liberally to interrogate the file system.
一直调试到StaticHandlerImpl#sendStatic方法,查看如何解析文件,并使用Debug expression窗口检查文件系统。
#4
2
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):
好的,我会回答我自己的问题。首先,我要指出的是,我已经关闭了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.
任何由静态处理程序处理的路径的请求都会导致文件系统或类路径上的文件被服务。默认的静态文件目录是webroot,但是可以配置它。
In the following example all requests to paths starting with /static/ will get served from the directory webroot:
在下面的示例中,所有以/static/开头的路径请求都将从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.
任何对根路径的请求都会导致索引页被服务。默认情况下,索引页面是index.html。这可以配置为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.
在我看来,如果我没有显式地为“/”定义一个处理程序,那么索引。html将隐式地提供服务。而且似乎仅仅创建一个静态服务器并将其添加到路由器就足够使用CSS/JS/IMG资源了。我认为我的假设是错误的。
So I added the following, which seem to be what was needed:
所以我补充了以下内容,这似乎是我们所需要的:
first, I told the StaticHandler explicitly to look for a "webroot" folder:
首先,我明确地告诉StaticHandler寻找一个“webroot”文件夹:
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();
});
#5
2
this solution worked for me, to add more details here is the folder structure for me
这个解决方案对我来说是有效的,在这里添加更多的细节是我的文件夹结构
src
main
java
com
-- java files with vertx
resources
webroot
html
js
#6
1
Static files kept in /src/main/resources/assets/
保存在/src/main/resources/assets/中的静态文件
ex: index.html
例:index . html
Router router = Router.router(vertx);
router.route("/files/*").handler(StaticHandler.create("assets"));
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
Now you can access it from http://localhost:8080/files/index.html
现在可以通过http://localhost:8080/files/index.html访问它
If you look at router.route("/files/*").handler(StaticHandler.create("assets"));
如果您查看route(“/files/*”).handler(StaticHandler.create(“assets”);
When a request matches /files/
it calls the StaticHandler which specifies assets
folder as the web-root
当请求匹配/文件/它调用StaticHandler时,它指定assets文件夹为web根目录
#7
1
Directory
目录
<AppRoot>/webroot/index.html
Vertx
Vertx
router.route("/app/*").handler(StaticHandler.create("webroot").setCachingEnabled(false));
URL
URL
http://127.0.0.1/app/index.html
Add the following Java VM options to enable hot reload
添加以下Java VM选项以启用热重载
-Dvertx.disableFileCPResolving=true
#8
0
I believe people are getting tripped up by the examples in the documentation for serving static resources at http://vertx.io/docs/vertx-web/kotlin/#_serving_static_resources -- some of the answers even go so far as to change the route path, which is not what is desired!
我相信在http://vertx上为静态资源提供服务的文档中的例子会让人们犯错误。io/docs/vertx-web/kotlin/#_serving_static_resources——有些答案甚至会更改路由路径,这不是我们想要的!
I have raised https://github.com/vert-x3/issues/issues/265 with details.
我已经提出了https://github.com/vert-x3/issues/issues/ issues/265详细信息。
Among others, OP tried:
其中,OP尝试:
http://localhost:8099/static/css/base.css
That should have worked, the rest of the paths were not valid, and src/main/resources/webroot
is a good place for packaged-up static resources.
这应该有效,其余路径无效,src/main/resources/webroot是打包静态资源的好地方。