Hi, I have started to work on the MERN stack application where I have created the Node JS-related files and connected the same to MongoDB. I have created a POST call that sends the JSON to the DB. I tried to test the API using postman but not sure what’s wrong, I have checked multiple times, the API throws bad requests. Please find attached a screenshot as a reference to the problem.
However, I am getting a successful response for GET requests on the same server.
Here is the code:
app.js
// Imports
require('dotenv').config()
const mongoose = require('mongoose');
const express = require('express');
const app = express();
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const authRoutes = require("./routes/auth");
// DB Connection
mongoose.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
}).then(() => {
console.log("DB CONNECTED!");
})
// Middlewares
app.use(bodyParser.json());
app.use(cookieParser());
app.use(cors());
// Routes
app.use("/api", authRoutes)
// Port
const port = process.env.PORT || 3000;
// Start Server Message
app.listen(port, () => {
console.log(`App is running at ${port}`);
});
auth.js
var express = require('express')
var app = express();
const router = express.Router()
const { signout, signup } = require("../controllers/authController")
router.post("/signup", signup);
router.get("/signout", signout);
module.exports = router;
authController.js
const User = require("../models/user");
exports.signup = (req, res) => {
const user = new User(req.body);
user.save((err, user) => {
if (err) {
return res.status(400).json({
err: "USER NOT SAVED!"
})
}
res.json(user);
})
}
exports.signout = (req, res) => {
res.json({
message: "User signout"
})
}
A similar problem was recently posted on Stackoverflow
Application: Postman Desktop for Apple Silicon
Version: v9.13.0
Any help on this is much appreciated. Thanks.