简体   繁体   English

Express中get/post/put/delete的路径参数

[英]Path parameter of get/post/put/delete in Express

I am a beginner in Express and I'm trying to figure out the path parameter in get/post/put/delete.我是 Express 的初学者,我正在尝试找出 get/post/put/delete 中的路径参数。

From the official documentations I get this description:从官方文档我得到这个描述:

" "

The path for which the middleware function is invoked;调用中间件 function 的路径; can be any of:可以是任何一个:

  • A string representing a path.表示路径的字符串。
  • A path pattern.路径模式。
  • A regular expression pattern to match paths.匹配路径的正则表达式模式。
  • An array of combinations of any of the above.以上任何一种组合的数组。

" "

And will be happy for further explanation, because I am trying to figure out an example where the path specified for post requests is some "/mydir" though no folder with name "mydir" is in the project and also the middleware function is declared right in the get/post/put/delete requests.并且很高兴进一步解释,因为我试图找出一个示例,其中为发布请求指定的路径是一些“/mydir”,尽管项目中没有名称为“mydir”的文件夹,并且中间件 function 被声明为正确的在 get/post/put/delete 请求中。 So things doesn't make sense yet regarding this official description.所以关于这个官方描述,事情还没有意义。

A code block from the project I'm reading:我正在阅读的项目中的代码块:

app.post("/quotes", (req, res) => {
  quotesCollection
    .insertOne(req.body)
    .then((result) => res.redirect("/"))
    .catch((error) => console.error(error));
});

So there is no "quotes" folder in the project, then what is the "/quotes" path paremeter referring to (If needed I will add the whole project)?所以项目中没有“quotes”文件夹,那么“/quotes”路径参数指的是什么(如果需要我将添加整个项目)?

// A string representing a path.
app.get('/', function (req, res) {
  res.send('hello world')
})

// A path pattern.
app.get('/ab?cd', function (req, res) {
  res.send('ab?cd')
})

// A regular expression pattern to match paths.
app.get(/.*fly$/, function (req, res) {
  res.send('/.*fly$/')
})

// Route parameters
app.get('/users/:userId/books/:bookId', function (req, res) {
  res.send(req.params)
})

Please read this for further explanation: The Parts of a URL: A Short & Sweet Guide请阅读此内容以获得进一步说明: URL 的部件:简短而甜蜜的指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM