何时在node.js中使用app.get(“/”,fun(res,req))和app.post(“/”,fun(res,req))?
我对 app.get()
和 app.post()
感到困惑。还有什么时候使用它们?
I am confused between app.get()
and app.post()
. Also when to use them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 HTTP 协议时,我们使用不同的 URL 来检索、添加、更新或删除信息。
为了指定我们要执行的操作,我们将 HTTP 方法(也称为 HTTP 动词)添加到 URL。
2 个流行的 HTTP 动词是:
在 ExpressJS 框架中,我们为应用程序应处理的每个可能的路由注册一个处理函数(称为中间件)。
要将处理函数注册到使用 GET 方法的路由,我们使用:
要将处理函数注册到使用 POST 方法的路由,我们使用:
When using the HTTP protocol, we use different URLs to retrieve, add, update or delete information.
To specify which action we want to perform we add to the URL the HTTP method (also called as HTTP verb).
2 popular HTTP verbs are:
In ExpressJS framework we register a handler function (known as a middleware) for each possible route that our app should handle.
to register a handler function to a route that uses the GET method, we use:
and to register a handler function to a route that uses the POST method, we use:
您好,在 API 中我们使用
Hi, in API we use
正如 @Kareem Adel 和 @Idan Krupnik 所说,这就是它们的用途,但它们的用途也取决于您实现它们的程度。 GET 方法仍然可用于 POST 方法,因此 DELETE PUT 和 PATCH 也可用于 DELETE PUT 和 PATCH,使用 HTTPS 动词的目的只是为了使我们的代码可读且更易于将来参考和同行使用。我们只是遵循语法,这很好。我的观点是不要对它们的本质感到困惑,只需遵守规则,但要知道规则是会被违反的,因此,如果您在另一个人的代码中看到在实现 POST 时调用 GET 方法,您不应该感到惊讶在回调函数中。
As said by @Kareem Adel and @Idan Krupnik, that is their uses but their usage is also dependent on how good you are to implement them. A GET method can still be used for POST method so also DELETE PUT and PATCH, using the HTTPS verbs for their intended is just to make our codes readable and more presentable for future references and your peers. We are just following syntaxes, which is good. My point is to not be confused with how they are, just stick to the rules but know that rules are there to be violated, so, you shouldn't be surprised if you see in another person's code calling GET method while POST is being implemented in the callback function.