调用Blog API时出现路由未找到错误求助(Express.js)
Let's break down how to fix this issue step by step—since your /api/v1/blogs/all endpoint works, we know the base setup (Express server, API prefix, and router mounting) is functional. The problem is almost certainly a mismatch between your request details and how the view/edit routes are defined.
1. Verify Route Definitions in Your Code
First, check your blog route file (likely routes/blogs.js in your repo) for how view and edit are configured:
- For a single blog view endpoint, it probably uses a route parameter to target a specific blog, like this:
If you’re requestingrouter.get('/view/:id', blogController.viewBlog);http://localhost:3000/api/v1/blogs/viewwithout providing anidparameter, Express will treat this as an undefined route and throw a 404. - For the
editendpoint, it’s almost certainly using a non-GET method likePUTorPATCH(since editing data shouldn’t use GET):
If you’re sending arouter.put('/edit/:id', blogController.editBlog);GETrequest to/editinstead of the correct method, you’ll get a 404 even if the path looks right.
2. Double-Check Your Postman Request Details
- URL Path: Ensure you’re including the required route parameter (e.g.,
http://localhost:3000/api/v1/blogs/view/123where123is a valid blog ID from your database). Missing this is the most common culprit here. - HTTP Method: Confirm you’re using the right method for each endpoint:
view: Almost alwaysGETedit: TypicallyPUTorPATCH(avoid usingGETorPOSTunless you explicitly defined it that way)
- API Prefix: Make sure you haven’t omitted
/api/v1/blogsfrom the path—for example, requestinghttp://localhost:3000/view/123instead of the full path will fail.
3. Debug with Express Logging
Add a simple debug middleware at the end of your server setup (right before the 404 handler) to log every incoming request. This will show you exactly what method and path Express is receiving, which can reveal mismatches:
// Add this before your 404 error handler app.use((req, res, next) => { console.log(`Received request: ${req.method} ${req.originalUrl}`); next(); }); // 404 handler app.use((req, res) => { res.status(404).json({ message: "Route not found" }); });
Run your server again, send the failing requests, and check the console. If the logged path/method doesn’t match what you defined in your routes, that’s your issue.
4. Confirm Router Mounting
Make sure your blog router is mounted correctly in the main server file (e.g., app.js or server.js):
const blogRoutes = require('./routes/blogs'); app.use('/api/v1/blogs', blogRoutes);
If the mount path is incorrect (like /api/blogs instead of /api/v1/blogs), your view/edit routes will be under the wrong prefix, leading to 404s.
Quick Example of Correct Requests
- For
view:- Method:
GET - URL:
http://localhost:3000/api/v1/blogs/view/60d21b4667d0d8992e610c85(replace with a valid ID from your database)
- Method:
- For
edit:- Method:
PUT - URL:
http://localhost:3000/api/v1/blogs/edit/60d21b4667d0d8992e610c85 - Body: Include the updated blog data in JSON format
- Method:
内容的提问来源于stack exchange,提问作者vaibhav naik

