node.js - 为什么我的 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;
谢谢 !
解决方案
推荐阅读
- java - 使用 args 时,不能从静态引用非静态
- javascript - 将 pdf.js 示例移植到电子
- c# - Unity - Oculus 距离抓取器不起作用?
- python - 调用列表值的变量 - python
- android - 时间戳(firebase)转换错误到日期实用程序
- ruby-on-rails - 在 Rails 中创建新用户时,我不断收到“密码不能为空”错误
- windows - 如何打开 postgres 命令行?
- javascript - Jquery vs Javascript设置一个html隐藏的布尔字段
- php - Codeigniter 中的 URI 路由和参数
- react-native - 如何在包 react-native-cookie 中按名称删除 cookie