TechSelfLearner
TechSelfLearner

Reputation: 397

POST request not working with JSON on POSTMAN

so I just began to learn some back end work, and I learned POSTMAN is a great tool for testing HTTP request methods. I have POSTMAN installed on my windows PC. I am running into a problem, in which whenever I want to test out POST method for creating an item, I just see "sending request" on POSTMAN without having a result.

My codes are the following,

const express = require('express');
const app = express();
const contacts = [{id:4}];
const contact_id = 1

const PORT = 3000;

app.get('/',(req,res)=>{
    res.send(`welcome to ${contacts}`)
})

app.post('/hello',(req,res)=>{
    const contact = {
        id:contact_id+1
    }
    contacts.push(contact);
})

app.listen(3000,()=>{
    console.log(`Server started on ${PORT}`)
})

What could be the problem?

Upvotes: 1

Views: 2562

Answers (1)

Kostas Minaidis
Kostas Minaidis

Reputation: 5412

As pzaenger correctly pointed out, you are never closing the connection on the server end with a res.end(), res.send() or res.json().

You need to close the connection with one of these methods, so that POSTMAN will receive your server's data (result):

app.post('/hello',(req,res)=>{
    const contact = {
        id: contact_id+1
    }
    contacts.push( contact );
    res.json( contacts );
});

Upvotes: 2

Related Questions