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

在Insomnia与Postman中调用POST /users/add接口时出现Cannot POST /users/add错误的技术求助

Hey there, let's troubleshoot that Cannot POST /users/add error step by step. Since you've confirmed MongoDB is connected, we can narrow our focus to Express routing and app setup. Here are the most likely issues and fixes:

1. Check how your user router is mounted in the main app

This is the #1 culprit for "Cannot POST" errors with nested routes. Your user router defines /add as a sub-route, but it only works if you've mounted the router to the /users path in your main app.js (or entry file).

Double-check that you have code like this in your main application file:

const express = require('express');
const app = express();
// Import your user router (adjust the path to match your file structure)
const userRoutes = require('./routes/userRoutes'); 

// First, enable JSON body parsing (critical for reading req.body)
app.use(express.json()); // Express 4.16+ has this built-in; no need for body-parser unless you're on an older version

// Mount the user router to the /users path
app.use('/users', userRoutes);

// 404 handler (must come AFTER all route mounts)
app.use((req, res) => {
  res.status(404).send(`Cannot ${req.method} ${req.path}`);
});

app.listen(3000, () => console.log('Server running on port 3000'));

If you accidentally mounted the router to a different path (like /api/users), your POST request would need to go to /api/users/add instead of /users/add.

2. Verify request URL exactness

Express routes are case-sensitive and strict by default (unless you change settings):

  • Make sure you're not adding a trailing slash to the URL (e.g., /users/add/ instead of /users/add)
  • Check that the URL uses all lowercase letters (e.g., /Users/add won't match /users/add)
  • If you've set app.set('strict routing', false), trailing slashes are allowed, but it's safer to match the route exactly as defined.

3. Confirm middleware order is correct

Middleware and route mounts run in the order they're defined. If you have a 404 handler or another middleware that sends a response before mounting your user router, the route will never be reached. Always mount your routes before any catch-all 404 or error-handling middleware.

4. Test the GET /users route first

To rule out general routing issues, send a GET request to /users. If that also returns a "Cannot GET" error, the problem is definitely with how you're mounting the user router. If the GET request works, double-check your POST route definition for typos (your code looks correct, but it's worth a second look!).

5. Ensure body parsing is enabled

While this won't cause a "Cannot POST" error, it will break your request once the route is matched. Your current code imports body-parser but doesn't use it in the router. Using express.json() globally (as shown in step 1) is the modern way to parse JSON request bodies in Express, so you can actually remove the body-parser import from your user router if you set up the global middleware.

Once you fix the router mounting issue, your POST request to /users/add should work as expected with the JSON body you're sending.

内容的提问来源于stack exchange,提问作者Eya Dkhil

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 17:07:32