javascript – Node.js – Express 4.x – 方法覆盖不处理PUT请求 - 代码日志

javascript – Node.js – Express 4.x – 方法覆盖不处理PUT请求

我试图让服务器处理PUT请求。但无济于事客户端在提交表单后仍然收到“Can not POST /”消息。我正在使用Express 4.x.

请注意,如果我在我的路线中更改“put”到“post”,则请求被处理得很好…

如何让我的服务器处理’PUT’请求?

服务器:

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

var app             = express();

app.use(bodyParser());
app.use(methodOverride());


app.get("/",function(req,res){
    res.render("index.ejs");
    console.log("GET received.");
});
app.put("/",function(req,res){
    console.log("PUT received: " + req.body.userName + " - " + req.body.password);
});

app.listen(1337);
console.log("Listening on 1337.");

客户

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
    </head>
    <body>
        <form action="/" method="post">
            First
            <input type="text" name="first">
            Last
            <input type="text" name="last">
            <input type="hidden" name="_method" value="put">
            <button type="submit">Submit</button>
        </form>
    </body>
</html>
更简单的方法可能是override using a query value

var methodOverride = require('method-override')

// override with POST having ?_method=PUT
app.use(methodOverride('_method'))

使用HTML的查询覆盖示例调用

<form method="POST" action="/resource?_method=PUT">
  <button type="submit">Put resource</button>
</form>
http://stackoverflow.com/questions/24019489/node-js-express-4-x-method-override-not-handling-put-request

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:javascript – Node.js – Express 4.x – 方法覆盖不处理PUT请求