節點。js -表達4。不處理PUT請求

[英]Node.js - Express 4.x - method-override not handling PUT request


I am attempting to have the server handle a PUT request. But to no avail. The client keeps receiving "Cannot POST /" message after submitting the form. I am using Express 4.x.

我正在嘗試讓服務器處理PUT請求。但無濟於事。客戶端在提交表單后不斷收到“不能發布/”消息。我用的是特快4.x。

Note that if I change "put" to "post" in my route, the request gets handled just fine...

注意,如果我將“put”改為“post”,請求就會得到很好的處理……

How can I have my server handle the 'PUT' request?

如何讓我的服務器處理“PUT”請求?

SERVER:

服務器:

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.");

CLIENT

客戶端

<!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>

4 个解决方案

#1


17  

A simpler way could be override using a query value:

更簡單的方法是使用查詢值來覆蓋:

var methodOverride = require('method-override')

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

Example call with query override using HTML :

使用HTML進行查詢覆蓋的示例調用:

<form method="POST" action="/resource?_method=PUT">
  <button type="submit">Put resource</button>
</form>

#2


12  

As of method-override v2.0.0 (release 2014-06-01), the default behaviour of the middleware does not check the POST body for a _method field; it only checks the X-HTTP-Method-Override header.

在method-override v2.0.0 (release 2014-06-01)時,中間件的默認行為不檢查POST body以獲取_method字段;它只檢查X-HTTP-Method-Override頭。

In order for method-override to operate as it did in previous versions, you need to provide a custom function to methodOverride, which is detailed on the project page:

為了讓method-override能夠像以前版本中那樣操作,您需要向method doverride提供一個自定義函數,該函數在項目頁面中有詳細說明:

custom logic

You can implement any kind of custom logic with a function for the getter. The following implements the logic for looking in req.body that was in method-override 1:

您可以使用getter的函數實現任何類型的自定義邏輯。下面實現了在req中查找的邏輯。身體在方法-覆蓋1:

var bodyParser     = require('body-parser')

var connect        = require('connect')
var methodOverride = require('method-override')

app.use(bodyParser.urlencoded())
app.use(methodOverride(function(req, res){
  if (req.body && typeof req.body === 'object' && '_method' in req.body) {
    // look in urlencoded POST bodies and delete it
    var method = req.body._method
    delete req.body._method
    return method
  }
}))

#3


0  

You can choose how to override:

你可選擇如何推翻:

// override with different headers; last one takes precedence

app.use(methodOverride('X-HTTP-Method'))          // Microsoft
app.use(methodOverride('X-HTTP-Method-Override')) // Google/GData
app.use(methodOverride('X-Method-Override'))      // IBM

#4


0  

We could also use a simple middleware to handle x-http-method-override

我們還可以使用一個簡單的中間件來處理x-http-method-override

     var router = express.Router();

     /**
     * Middleware that detects a HTTP method tunneled,
     * inside the header of another HTTP method. Detects
     * and routs to the method mentioned in the header.
     */

    router.use((req,resp,next)=>{
        if(req.headers['x-http-method-override']){
            req.method = req.headers['x-http-method-override'];
        }
        next();
    });

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/06/03/725a34902ebc20fc150f1cf92b26e3fe.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com