首页 > 解决方案 > 为什么我的 POST 请求仅适用于调试器?

问题描述

我尝试编写 CRUD 并使用 Postman 进行检查。

当我在发布请求结束时使用调试器时,它正在工作。

问题:当我删除调试器并尝试运行代码时,没有任何反应——只有错误:“无法得到任何响应”。在没有调试器的情况下如何使我的帖子正常工作?

***例如,我通过邮递员将其作为 JSON 发送,如下所示:

{   
    "id":3,
    "name": "Ron",
    "phone": "01323133333"
}

这是相关代码:

index.js:

const express = require('express');
const app = express();
db = require('./db');
app.use(express.static('public'));
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const port = 1400;
app.listen(port,()=>{
    console.log(`Server is running on port ${port}`);
})

//post request:
app.post('/user' , (req,res)=> {
    const {body} = req,
    {id,name,phone} = body

    db.query(`INSERT INTO public.users(
        id, name, phone)
        VALUES (${id}, '${name}', '${phone}');`,(err,dbRes)=>{
            if(err)
            res.status(400).send(err);

            else
            {
                res.send(dbRes.rows);
            }
        })
})

db.js:

const {Client} = require ('pg');

const client = new Client ({
    user:'postgres',
    host:'localhost',
    database:'nodeapp',
    password:'123456',
    port:5432
  })

  client.connect();

module.exports = client;

谢谢 !

标签: node.jspostmancrud

解决方案


推荐阅读