iOS Vapor Js 未找到

问题描述 投票:0回答:1

我的iOS项目。我通过在基本结构中添加“服务器”文件夹来添加 Vapor 代码。

import Vapor
import Leaf

extension URL {
    static func documentsDirectory() throws -> URL {
        try FileManager.default.url(for: .documentDirectory,
                                    in: .userDomainMask,
                                    appropriateFor: nil,
                                    create: false)
    }

    func visibleContents() throws -> [URL] {
        try FileManager.default.contentsOfDirectory(at: self,
                                                    includingPropertiesForKeys: nil,
                                                    options: .skipsHiddenFiles)
    }  
}

class FileServer {
    // 1
    var app: Application
 
    // 2
    let port: Int

    init(port: Int) {
        self.port = port
    
        // 3
        app = Application(.development)
    
        // 4
        configure(app)
    }

    private func configure(_ app: Application) {
        // 1
        app.http.server.configuration.hostname = "0.0.0.0"
        app.http.server.configuration.port = port
    
        // 2
        app.views.use(.leaf)
    
        // 3
        app.leaf.cache.isEnabled = app.environment.isRelease
    
        // 4
        app.leaf.configuration.rootDirectory = Bundle.main.bundlePath
    
        // 5
        app.routes.defaultMaxBodySize = "50MB"
    }

    func start() {
        // 1
        Task(priority: .background) {
            do {
                try app.register(collection: FileWebRouteCollection())
            
                // 2
                try app.start()
            } catch {
                fatalError(error.localizedDescription)
            }
        }
    }
}

上面是代码“FileServer”。

import Vapor

struct FileWebRouteCollection: RouteCollection {

    func boot(routes: RoutesBuilder) throws {
        routes.get(use: filesViewHandler)
    }

    func filesViewHandler(_ req: Request) async throws -> View {
        let documentsDirectory = try URL.documentsDirectory()
        let fileUrls = try documentsDirectory.visibleContents()
        let filenames = fileUrls.map { $0.lastPathComponent }
        let context = FileContext(filenames: filenames)
        return try await req.view.render("index", context)
    }
}

struct FileContext: Encodable {
    var filenames: [String]
}

struct FileUploadPostData: Content {
    var file: File
}

上面是代码“FileWebRouteCollection”。

<!doctype html>
<html>
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Test</title>

<style>
    html {width:100%; height:100%;}
    body {width:100%; height:100%; margin:0;}
</style>

hi

<script type="text/javascript" src="./jsForders/jsFile.js"></script>

</head>
<body>
    <div id="div_canvas" style="width:100%; height:100%;"></div>
</body>
</html>

上面是代码“index.leaf”。

当我运行应用程序并进入vapor告诉我的地址时,我无法识别javascript文件。我该怎么办??

class ViewController: UIViewController {

    var server = FileServer(port: 1206)

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    
        self.server.start()
    }
}

vapor 服务器在视图控制器上运行,如上所述。

ios swift vapor
1个回答
0
投票

您需要启用

FileMiddleware
才能提供静态文件或为每个要提供服务的文件注册一个路由。看来你也没有

© www.soinside.com 2019 - 2024. All rights reserved.