ISSUE: I am encountering a problem while making a POST request in Postman. The application keeps sending requests without receiving any response.
I have read through various solutions for Postman hanging during a POST request, but none of them seem to solve my specific problem.
GET REQUEST IS FUNCTIONING PROPERLY
https://i.stack.imgur.com/Yiugd.png
Below is the request that I submitted:
{
"title": "This is title",
"description":"This is my first RestfulAPI"
}
https://i.stack.imgur.com/DdhEW.png
I have three files named Post.js, posts.js, and app.js
Post.js
const mongoose = require('mongoose');
//Creating a schema
const PostSchema= mongoose.Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
}
})
module.exports=mongoose.model('Posts',PostSchema);
posts.js
const express= require('express');
const router=express.Router();
const Post= require('../models/Post');
router.get('/',(req,res) => {
res.send('We are on posts');
});
router.get('/specific',(req,res) => {
res.send('Specific posts');
});
router.post('/',async (req,res)=>{
console.log(req.body);
const post= new Post({
title: req.body.title,
description: req.body.description
});
try{
const savedPost = await post.save();
res.json(savedPost).status(3000).end();
}catch(err){
res.json({message: err}).status(3000).end();
console.log('Something is wrong');
}
});
module.exports= router;
app.js
const express = require('express');
const mongoose= require('mongoose');
const app= express();
const bodyParser= require('body-parser');
require('dotenv/config');
app.use(bodyParser.json());
const postsRoute = require('./routes/posts');
app.use('/posts',postsRoute);
app.get('/',(req,res) => {
res.send('We are on home');
});
mongoose.connect(process.env.DB_CONNECTION,{ useNewUrlParser: true,useUnifiedTopology: true },() =>
console.log('connected to DB!')
);
app.listen(3000);
The output on my console after sending the POST request https://i.stack.imgur.com/XMqEi.png