发出POST请求时连接终止?

时间:2019-03-06 05:22:34

标签: javascript postgresql postman

我想知道是否有人可以在这里帮助我。我目前正在学习有关如何将数据库连接到服务器的Udemy课程。我一直在逐字逐句地检查老师的代码,经常检查我的代码是否有小错误。到目前为止,我的server.js文件中的代码没有任何问题。端口运行平稳。但是,当我运行Postman的POST请求时,我得到了

  

未处理的拒绝错误:连接意外终止

通过邮递员,我完全按照老师的所作所为。 POST请求包含电子邮件,密码,名称,并且localhost:3000 / register路径正确。这就是我的服务器所连接的。我想知道发生了什么事情,因为我的代码运行平稳,直到我执行POST请求为止。还注意到我在邮递员上收到200 OK响应,但在服务器上却收到未处理的拒绝错误消息。

enter image description here

是的,控制台日志是有意的,我正按照他在视频中所做的工作,因此下个视频的代码肯定会随着时间而改变。

const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require ('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');
const pg = require('pg');

const db = knex({
  client: 'pg',
  connection: {
    host : '127.0.0.1',
    user : 'postgres',
    port: '3000',
    password : '',
    database : 'smart-brain'
  }
});




const app = express();

const database = {
  users: [
    {
      id: '123',
      name: 'Jess',
      email: 'jess@gmail.com',
      password: 'cookies',
      entries: 0,
      joined: new Date()
    },
     {
      id: '124',
      name: 'Sally',
      email: 'sally@gmail.com',
      password: 'bananas',
      entries: 0,
      joined: new Date()
    }
  ]
}


app.use(bodyParser.json());
//body parser is basically json.parse. we want to always parse json so our code is readable in string form. POST > Raw > JSON
app.use(cors())

app.get('/', (req, res)=> {
        res.send(database.users);
        })

app.post('/signin', (req, res) => {
  if(req.body.email === database.users[0].email && req.body.password === database.users[0].password) {
    res.json(database.users[0])
  } else {
    res.status(400).json('error logging in')
  }
})

app.post('/register', (req, res) => {
  const { email, name, password } = req.body;
  db('users').insert({
    email: email,
    name: name,
    joined: new Date() 
  }).then(() => console.log())
  res.json(database.users[database.users.length-1])
});


app.get('/profile/:id', (req, res) => {
  const { id } = req.params;
  let found = false;
  database.users.forEach(user => {
    if (user.id === id) {
      found = true;
      return res.json(user);
    } 
  })
  if (!found) {
    res.status(400).json('not found...')
  }
})

//now we are creating route for entries count. everytime they submit image, they will get a count for it
app.put('/image', (req, res) => {
  const { id } = req.body;
  let found = false;
  database.users.forEach(user => {
    if (user.id === id) {
      found = true;
      user.entries++
      return res.json(user.entries);
    } 
  })
  if (!found) {
    res.status(400).json('not found...')
  }
})

app.listen(3000, ()=> {
  console.log('app is running on port 3000');
})

1 个答案:

答案 0 :(得分:0)

然后在阻止之后尝试添加catch函数。

app.post('/register', (req, res) => {
const { email, name, password } = req.body;
  db('users').insert({
    email: email,
    name: name,
    joined: new Date() 
  }).then(() => {
    res.json(database.users[database.users.length-1])   
  }).catch((err)=>{console.log(err)})

});