Express中Get/Post/Put/Delete路径参数新手疑问求助
Hey there! As someone who’s been working with Express for a while, I totally get why this might confuse you at first—let’s clear this up step by step.
First off, the route path in Express has nothing to do with physical folders on your server. It’s purely a logical identifier that uses URL structure to match incoming HTTP requests to the right code.
Let’s break down your example code:
app.post("/quotes", (req, res) => { quotesCollection.insertOne(req.body) .then((result) => res.redirect("/")) .catch((error) => console.error(error)); });
Here’s what /quotes actually means:
- When a client (like a frontend form, Postman, or any other HTTP client) sends a
POSTrequest to a URL likehttp://your-server-domain/quotes, Express checks all its registered routes and matches this request to the function you’ve defined here. - It doesn’t look for a folder named
quoteson your server—this path is just a way to label which requests should trigger this specific code (in your case, inserting a new quote into a database and redirecting the user).
To tie this back to the official docs you mentioned: /quotes is a simple string path, one of the allowed types (along with path patterns, regex, or arrays of these). Express uses these paths to route traffic to the right handlers, regardless of your server’s file system structure.
A quick side note: The only time Express paths map to folders is when you use express.static to serve static files (like HTML, CSS, or images). But that’s a separate feature—your app.post("/quotes") route is a custom business logic route, not tied to any physical files.
内容的提问来源于stack exchange,提问作者user12421682

