Method-override? in the actual combat of nodejs But it doesn't work. What's the problem?


const connect = require("connect");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const methodOverride = require("method-override");

const edit = function(req, res, next) {
  if ("GET" != req.method) {
    return next();
  }
  res.setHeader("Content-Type", "text/html");
  res.write("<form method="POST">");
  res.write("<input type="text" name="user[name]" value="allen"/>");
  res.write("<input type="hidden" name="_method" value="PUT"/>");
  res.write("<input type="submit" value="update" />");
  res.write("</form>");
  res.end();
};
const update = function(req, res, next) {
  console.log("-->", req.method, req.originalMethod, req.body);
  if ("PUT" != req.method) {
    return next();
  }
  res.end("update name to " + req.body.user.name);
};
connect()
  .use(morgan("dev"))
  .use(bodyParser.urlencoded({ extended: false }))
  .use(methodOverride("_method", { methods: ["post", "get"] }))
  .use(edit)
  .use(update)
  .listen(3000, function() {
    console.log("the server start at 3000");
  });
Oct.13,2021
Menu