node.js Connect.js methodOverride 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8378338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:50:56  来源:igfitidea点击:

What does Connect.js methodOverride do?

node.jsconnect.js

提问by Randomblue

The Connect.js very terse documentationsays methodOverride

Connect.js非常简洁的文档methodOverride

Provides faux HTTP method support.

提供虚假的 HTTP 方法支持。

What does that mean? The obvious Google searchis less than helpful. Why is methodOverrideuseful?

这意味着什么?在明显的谷歌搜索不到有帮助的。为什么methodOverride有用?

回答by alessioalex

  • If you want to simulate DELETEand PUT, methodOverrideis for that.
  • If you pass in the _method post parameter set to 'delete'or 'put', then you can use app.deleteand app.putin Express instead of using app.postall the time (thus more descriptive, verbose):
  • 如果你想模拟DELETEand PUTmethodOverride就是为了那个。
  • 如果您将 _method post 参数设置为'delete''put',那么您可以在 Express 中使用app.deleteandapp.put而不是一直使用app.post(因此更具描述性、更详细):

Backend:

后端:

// the app
app.put('/users/:id', function (req, res, next) {
  // edit your user here
});

Client logic:

客户端逻辑:

// client side must be..
<form> ...
  <input type="hidden" name="_method" value="put" />
</form>