I am making a simple register login auth and was wanting to try postman to test it (basically make a POST request to create a new user on mongodb). The problem arose when I logged req.body
and saw that it was blank. Postman was not sending the right request to the localhost.
I tried the most basic example which too wasnt working:
app.post('/', (req, res) => {
return res.status(200).json(req.body)
})
the corresponding postman request:
The body is set to x-www-form-urlencoded and to be sure here is server.js
:
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(bodyParser.json());
// DB Config
const db = require("./config/keys").mongoURI;
// Connect to MongoDB
mongoose
.connect(
db,
{ useNewUrlParser: true }
)
.then(() => console.log("MongoDB successfully connected"))
.catch(err => console.log(err));
// Passport middleware
app.use(passport.initialize());
// Passport config
require("./config/passport")(passport);
// Routes
app.use("/api/users", users);
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server up and running on port ${port}`));
app.post('/', (req, res) => {
return res.status(200).json(req.body)
})
The problem seems to be very minute but I don’t know what to do