【发布时间】:2017-05-12 16:58:45
【问题描述】:
我有以下 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.
【问题讨论】: