正如其他人建议的那样,我建议远离模板,自己创建一个干净的项目。
创建此文件夹结构:
MyAPI
├── Package.swift
└── Sources
└── main.swift
然后,在Package.swift文件中
import PackageDescription
let package = Package(
name: "MyAPI",
targets: [],
dependencies: [
.Package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", majorVersion: 2)
]
)
还有 main.swift 文件:
import PerfectHTTP
import PerfectHTTPServer
do {
let route = Route(method: .get, uri: "/hello", handler: { (request: HTTPRequest, response: HTTPResponse) in
response.appendBody(string: "world!")
response.completed()
})
try HTTPServer.launch(.server(name: "localhost", port: 8080, routes: [route]))
} catch {
fatalError("\(error)")
}
转到命令行并运行:
swift package generate-xcodeproj
打开生成的项目文件:
MyAPI.xcodeproj
更改活动方案,然后构建并运行:
![在此处输入图像描述](https://i.stack.imgur.com/PNNOv.png)
在 Safari 中打开:
http://localhost:8080/hello