简体   繁体   中英

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?

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 );
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM