0

I am trying to create an api and send post request using the postman, but for some reason I'm not getting the data which is sent through the post man. Instead when I cancel the postman send request I get response which is shown in the screenshot.

Postman Screenshot: Postman, post request screenshot

nodejs server response screenshot: Response I'm getting after I cancel the Postman send request

server.js

const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');  
const cors = require('cors'); 

// importing custom created modules
const config = require('./config');

// Instantiating express
const app = express();

// Connecting to the mongodbLab
mongoose.connect(config.database, { useNewUrlParser: true }, (error) => {
    if (error) {
        console.log('Error :',error);
    } else {
        console.log('Connected to the database.');
    }
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(morgan('dev'));
app.use(cors);

const userRoutes = require('./routes/account');
app.use('/api/accounts', userRoutes);

// Running the server at port 3000
app.listen(config.port, error => {
    console.log('Server started with config file at Port: ' + config.port);
});

account.js

const router = require('express').Router();
const jwt = require('jsonwebtoken'); // Encrypts user data.

router.post('/signup', (req, res, next) => {
    console.log(req.body);
});

module.exports = router;

I tried even changing from https://localhost:3000 to http://localhost:3000 as suggested in this post Here

And even tried adding res.end() as suggested here and here. But nothing is working, If I goto localhost:3000 the server just keeps loading infinitely. Please help!

Shadab Umer
  • 173
  • 3
  • 12

1 Answers1

0

Okay I got the solution, it was because of app.use(cors). I don't know why but when I commented out this line everything started to work perfectly. But in the response although I get the json object. But there is an array of Object, which I didn't get.

Here's the response screenshot: enter image description here

Edit: I finally got it I didn't call the cors() method properly. It should have been app.use(cors()), but I called it as just app.use(cors) missing paranthesis(). Now Everything is working fine.

Shadab Umer
  • 173
  • 3
  • 12