?_method = DELETE无法使用Express / Node应用程序?

时间:2017-01-12 17:58:08

标签: javascript html express

我参加了Colt Steele的网络开发课程,我试图弄清楚这一点。

以下是我尝试删除博文时遇到的错误:

Cannot DELETE /blogs/587445324a978708968f451a?_method=DELETE

这里是app.js的代码:

var bodyParser      =   require("body-parser"),
    methodOverride  =   require("method-override"),
    mongoose        =   require("mongoose"),
    express         =   require("express"),
    app             =   express();

//APP CONFIG
mongoose.connect("mongodb://localhost/restful_blog_app");
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride("_method"));

...

// DELETE ROUTE
app.delete("/blog/:id", function(req , res){
    Blog.findByIdAndRemove(req.params.id, function(err){
        if(err){
            res.redirect("/blogs");
        } else {
            res.redirect("/blogs");
        }
    });
});

这是show.ejs的代码:

<% include ./partials/header %>
<div class="ui main text container segment">
    <div class="ui huge header"><%= blog.title %></div>
    <div class="ui top attached">
        <div class="item">
            <img class="ui centered rounded image" src="<%= blog.image %>">
            <div class="content">
                <span><%= blog.created.toDateString() %></span>                
            </div>
            <div class="description">
                <p><%- blog.body %></p>
            </div>
            <form action="/blogs/<%= blog._id %>?_method=DELETE" method="POST">
                <button class="ui red basic button">Delete</button>
            </form>
        </div>
    </div>
</div>
<% include ./partials/footer %>

0 个答案:

没有答案