簡體   English   中英

POST 請求不適用於 POSTMAN 上的 JSON

[英]POST request not working with JSON on POSTMAN

所以我才開始學習一些后端工作,我了解到 POSTMAN 是一個很好的測試 HTTP 請求方法的工具。 我的 Windows PC 上安裝了 POSTMAN。 我遇到了一個問題,每當我想測試用於創建項目的 POST 方法時,我只會在 POSTMAN 上看到“發送請求”而沒有結果。

我的代碼如下,

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

可能是什么問題呢?

正如pzaenger正確指出的那樣,您永遠不會使用res.end()res.send()res.json()關閉服務器端的連接。

您需要使用以下方法之一關閉連接,以便 POSTMAN 會收到您服務器的數據(結果):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM