You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Blog API时出现路由未找到错误求助(Express.js)

Troubleshooting "Route Not Found" for view/edit API Endpoints

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:
    router.get('/view/:id', blogController.viewBlog);
    
    If you’re requesting http://localhost:3000/api/v1/blogs/view without providing an id parameter, Express will treat this as an undefined route and throw a 404.
  • For the edit endpoint, it’s almost certainly using a non-GET method like PUT or PATCH (since editing data shouldn’t use GET):
    router.put('/edit/:id', blogController.editBlog);
    
    If you’re sending a GET request to /edit instead 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/123 where 123 is 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 always GET
    • edit: Typically PUT or PATCH (avoid using GET or POST unless you explicitly defined it that way)
  • API Prefix: Make sure you haven’t omitted /api/v1/blogs from the path—for example, requesting http://localhost:3000/view/123 instead 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)
  • For edit:
    • Method: PUT
    • URL: http://localhost:3000/api/v1/blogs/edit/60d21b4667d0d8992e610c85
    • Body: Include the updated blog data in JSON format

内容的提问来源于stack exchange,提问作者vaibhav naik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:33:42