i have a problem when POST some data in POSTMAN my problem is during "post" method /auth/login req.body return empty array.
Postman return empty object only if i use POST method with use form-data, if i change to xxx-www-form-urlencoded whatever works fine. I wanna know why it works so
const express = require('express');
const mongoose = require('mongoose');
const app = express();
require('dotenv').config();
const port = process.env.PORT || 5000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/static", express.static(__dirname + "/assets"))
app.post('/auth/login', (req, res) => {
res.status(200).json(req.body)
})
mongoose.connect("mongodb://localhost:27017")
.then(() => {
app.listen(port, () => {
console.log('port' + port)
})
})
2
Answers
I solved this, i just created multer in my controller and it work how i wanted
I’m assuming you mean
application/x-www-form-urlencoded
instead ofxxx-www-form-urlencoded
and you meanmultipart/form-data
instead ofform-data
.These 2 content-types are completely different encodings. When you called:
You added a middleware that can parse
application/x-www-form-urlencoded
, but that does not mean it automatically parses other formats too. It’s only for that format, just likeexpress.json()
is only for theapplication/json
format.