简体   繁体   中英

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.

From the official documentations I get this description:

"

The path for which the middleware function is invoked; 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. 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)?

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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