页面存在时出现 404 错误

Getting a 404 error when the page exists

我正在向具有 HTML 表单的服务器发出 post 请求。当我按下表单的提交按钮时,出现 404 页面不存在错误。但是,如果我通过直接在地址栏中输入来转到 URL,我会得到应该从服务器得到的错误(400 无效请求)。

这是我的HTML(注意我使用的是叶子):

#extend("base")

#export("head") {
    <link rel="stylesheet" href="/styles/normalize.css">
    <link rel="stylesheet" href="/styles/skeleton.css">
}

#export("body") {
    <div class="container">
        <form action="/admin/new-post" method="POST">
            <div class="row" style="padding: 25px 0 0 0;">
                <div class="six columns form-group">
                    <label for="email">Usernam</label>
                    <input class="u-full-width" type="text" placeholder="Username" id="email" name="email">
                </div>
                <div class="six columns form-group">
                    <label for="password">Password</label>
                    <input class="u-full-width" type="password" placeholder="Password" id="password" anem="password">
                </div>
            </div>
            <input type="hidden" name="_csrf" value="{{csrfToken}}">
            <button type="submit" class="button-primary">Login</button>
        </form>
    </div>
}

我的服务器代码 (Swift):

import Vapor
import HTTP

final class AdminController {

    func addRoutes(to drop: Droplet) {
        let admin = drop.grouped("admin")
        drop.get("login", handler: login)
        admin.get("new-post", handler: newPost)
    }

    func login(_ request: Request)throws -> ResponseRepresentable {
        return try drop.view.make("admin-login", [])
    }

    func newPost(_ request: Request)throws -> ResponseRepresentable {
        guard let cred = request.auth.header?.basic else {
            throw Abort.badRequest
        }
        do {
            try request.auth.login(cred)
            return try drop.view.make("new-post", [])
        } catch {
            return Response(redirect: "/login?login=failed")
        }
    }
}

为什么我会收到 404 错误?

我使用的网络框架是Vapor。

所以,看看你的 Droplet,你添加了一条带有 GET-请求 new-post 的新路由,但你的表单尝试 POSTnew-post,肯定会失败。

尝试改变

admin.get("new-post", handler: newPost)

admin.post("new-post", handler: newPost)