1

当我单击添加按钮时,它显示错误:`{error: true, reason: "Not Found"}

configure.swift代码:

import FluentSQLite
import Vapor
import Leaf // added
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
    // Register providers first
    try services.register(FluentSQLiteProvider())
    
    // Register routes to the router
    let router = EngineRouter.default()
    try routes(router)
    services.register(router, as: Router.self)
    
    // Register middleware
    var middlewares = MiddlewareConfig() // Create _empty_ middleware config
    // middlewares.use(FileMiddleware.self) // Serves files from `Public/` directory
    middlewares.use(ErrorMiddleware.self) // Catches errors and converts to HTTP response
    services.register(middlewares)
    
    let directoryConfig = DirectoryConfig.detect()
    services.register(directoryConfig)
    let sqlite = try SQLiteDatabase(storage: .file(path:"\(directoryConfig.workDir)pizza.db"))
    var databases = DatabasesConfig()
    databases.add(database: sqlite, as: .sqlite)
    services.register(databases)
    
    var migrations = MigrationConfig()
    migrations.add(model: Todo.self, database: .sqlite)
    migrations.add(model: Pizza.self, database: .sqlite)
    services.register(migrations)
    
    let leafProvider = LeafProvider()    // added
    try services.register(leafProvider)  // added
    config.prefer(LeafRenderer.self, for: ViewRenderer.self)// added
    let serverConfigure = NIOServerConfig.default(hostname: "0.0.0.0", port: 9090)
    services.register(serverConfigure)
}

pizza.swift代码:

import Foundation
import Vapor
import FluentSQLite

struct Pizza: SQLiteModel, Content, SQLiteMigration, Migration {
    var id:  Int?
    var name: String
    var description: String
    var  price: Int
}

routes.swift代码:

import Routing
import Vapor
import FluentSQLite
public func routes(_ router: Router) throws {
    router.get { req -> Future <View> in
        let Newyorker = Pizza(id: 5, name: "statinisland", description: "impracticaljokers", price: 55)
        let Traditional = Pizza(id: 5, name: "rome", description: "pope", price: 55)
        return try req.view().render("welcome",["pizza":[Newyorker,Traditional]])
    }
    router.post(Pizza.self, at: "add") { req, pizza in
        return pizza
    }
}

`welcome.leaf 截图:

在此处输入图像描述

localhost截屏:

在此处输入图像描述

error截屏:

在此处输入图像描述

想不通。谢谢。

编辑 1. 我已经粘贴了叶子文件 -

 <!DOCTYPE html>
 <html>
 <body>
  <h1> Pizza </h1>
  <p> Welcome to best pizza in the  world  </p>
 <ul>
   #for(pizza in pizza)  {
  <li>(pizza.name)</li>
  }
  </ul>
 </body>
 </html>

 <form method="post" action="/add">
 <p>Name: <input type="text" name = "name" /></p>
 <p>Descrition: <input type="text" name = "description" /></p>
 <p>Price: <input type="number" name = "price" /></p>
 <button type="submit">Add</button>
 </form>
4

0 回答 0