Node.js - Express 4.x - method-override not handling PUT request
express, javascript, node.js
Solution
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 :
<form method="POST" action="/resource?_method=PUT">
<button type="submit">Put resource</button>
</form>
Problem
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. Note that if I change "put" to "post" in my route, the request gets handled just fine... How can I have my server handle the 'PUT' request? 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> ```