gpt4 book ai didi

java - vert.x 提供静态文件

转载 作者:行者123 更新时间:2023-11-29 08:36:16 25 4
gpt4 key购买 nike

我有以下 Verticle 类:

public class SendFileExample extends AbstractVerticle {

public void start(Future<Void> fut) throws Exception {
Router router = Router.router(vertx);
router.route("/hello").handler(StaticHandler.create("client"));

router.route("/hello").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
System.out.println("Hello");
response.sendFile("client/index.html");
});

vertx.createHttpServer().requestHandler(router::accept).listen(3000,
result -> {
if (result.succeeded()) {
fut.complete();
} else {
fut.fail(result.cause());
}
}
);
}
}

我的 html 文件是:

<html>
<head>
<title> hello </title>
</heade>
<body>
<h1> Hello World </h1>
<button> Hello </button>
<script src="app.js"></script>
</body>
</html>

我使用“StaticHandler.create...”来为客户端文件夹中的所有静态文件提供服务。如您所知,我希望一旦服务器收到对“localhost:3000/hello”的 GET 请求,客户端将获得一个 HTML 页面,该页面将调用 app.js 文件。

很遗憾,我做不到。 index.html 已加载,浏览器无法加载 app.js。

重要的是要注意 index.html 和 app.js 都位于完全相同的路径中,即 ${PROJECT_ROOT}/client。

然而,代码位于:${PROJECT_ROOT}/src/main/java/com/company.

最佳答案

您在定义静态处理程序时只是错过了星号:

router.route("/hello*").handler(StaticHandler.create("client"));

关于java - vert.x 提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43942945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com