首页 > 解决方案 > 在 Vapor 中将根指向静态网页

问题描述

我有一个静态网页存储在我的 Vapor 服务器的 Public 文件夹中。它有一个 index.html 文件。但是,当我导航到 root (http://localhost:8080) 时,它会显示Not found.

我需要做什么使根指向 index.html?

标签: vapor

解决方案


对于蒸汽 4 ...

在 routes.swift 内部:

app.get { req -> EventLoopFuture<View> in
    return req.view.render(app.directory.publicDirectory + "index.html")
}

这假设您在项目目录根目录中有一个“Public”文件夹,其中包含一个 index.html 文件。

此外,在 configure.swift 中:

app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))

构建项目,运行服务器,并将浏览器指向 localhost:8080,您甚至不必指定 localhost:8080/index.html,它就可以了。


推荐阅读