NodeJS Express POST请求无法获取请求体数据求助
req.body Undefined/Empty in Express POST Requests Hey there! I totally get how frustrating it is when you’re sending POST requests to your Express server but req.body keeps coming up empty or undefined. Let’s walk through the most likely fixes— I’ve dealt with this exact issue plenty of times, so these should get you sorted:
1. You’re Missing Request Body Parsing Middleware
Express doesn’t automatically parse POST request bodies—you have to explicitly add middleware for that, based on the type of data you’re sending:
- For JSON payloads (the most common format for API requests):
Add this line before your routes are defined:app.use(express.json()); - For form data (
application/x-www-form-urlencoded):
Use this middleware instead:
Theapp.use(express.urlencoded({ extended: true }));extended: trueoption lets you parse nested objects in form data, which is super useful for complex submissions.
Critical reminder: Make sure this middleware is placed before any of your POST routes. If your routes are defined first, the request will hit the route before the body gets parsed!
2. Your Request’s Content-Type Header Is Mismatched
Even if you have the right middleware, Express won’t parse the body unless the request’s Content-Type matches what the middleware expects:
- If you’re sending JSON, your client must include the header:
Content-Type: application/json - If you’re sending standard form data, use
Content-Type: application/x-www-form-urlencoded - For file uploads or
multipart/form-datarequests, skip to point 4—default middleware can’t handle these.
3. Your CORS Middleware Is Incomplete
Looking at your code snippet, your CORS headers are cut off. To make sure the browser allows sending the Content-Type header (which is required for body parsing), update your CORS middleware to include allowed headers:
app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); // Handle OPTIONS preflight requests (browsers send these before actual POST requests) if (req.method === 'OPTIONS') { return res.sendStatus(200); } next(); });
Without allowing Content-Type in Access-Control-Allow-Headers, the browser might block sending that header entirely, leading to an unparsed empty body.
4. You’re Sending multipart/form-data (File Uploads)
If your POST request includes files or uses multipart/form-data, the default Express middleware won’t parse it. You’ll need to use the multer package:
- First install it via npm:
npm install multer - Then configure it in your code:
const multer = require('multer'); const upload = multer(); // This configures multer to not save files to disk (just parse the form data) // Use upload.none() for requests without files, or upload.single('file') for single file uploads app.post('/api/your-endpoint', upload.none(), (req, res) => { console.log(req.body); // Now you’ll see your form data here! });
Quick Verification Checklist
- Added
express.json()/express.urlencoded()middleware before your routes - Client sends the correct
Content-Typeheader matching your payload - CORS middleware explicitly allows the
Content-Typeheader - For
multipart/form-datarequests, used themulterpackage
Give these steps a try—9 times out of 10, one of these will fix the req.body issue!
内容的提问来源于stack exchange,提问作者Aplex

