postgresql - 如何将 knex.js 连接到 PostgresSQL 数据库?
问题描述
嗨,我正在尝试使用 Knex.js 将数据库连接到 server.js 我尝试将用户添加为 postgresql,并且我还尝试将主机添加为 localhost,但这没有用,我总是得到
加载资源失败:服务器响应状态为 400(错误请求)
以下是我尝试注册时的错误快照!
下面是我的 register.js,它应该有助于重新注册到数据库!
const handleRegister = (req, res, db, bcrypt) => {
const { email, name, password } = req.body;
if (!email || !name || !password) {
return res.status(400).json('incorrect form submission');
}
const hash = bcrypt.hashSync(password);
db.transaction(trx => {
trx.insert({
hash: hash,
email: email
})
.into('login')
.returning('email')
.then(loginEmail => {
return trx('users')
.returning('*')
.insert({
email: loginEmail[0],
name: name,
joined: new Date()
})
.then(user => {
res.json(user[0]);
})
})
.then(trx.commit)
.catch(trx.rollback)
})
.catch(err => res.status(400).json('unable to register'))
}
module.exports = {
handleRegister: handleRegister
};
下面是我的 server.js 文件!
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');
const register = require('./controllers/register');
const signin = require('./controllers/signin');
const profile = require('./controllers/profile');
const image = require('./controllers/image');
const db = knex({
client: 'pg',
connection: {
host : 'localhost',
user : 'postgres',
database : 'smartbrain1'
}
});
const app = express();
app.use(cors())
app.use(bodyParser.json());
app.get('/', (req, res)=> { res.send(db.users) })
app.post('/signin', signin.handleSignin(db, bcrypt))
app.post('/register', (req, res) => { register.handleRegister(req, res, db, bcrypt) })
app.get('/profile/:id', (req, res) => { profile.handleProfileGet(req, res, db)})
app.put('/image', (req, res) => { image.handleImage(req, res, db)})
app.post('/imageurl', (req, res) => { image.handleApiCall(req, res)})
app.listen(3000, ()=> {
console.log('app is running on port 3000');
})
这是我在终端的 postgreSQL 中创建的数据库作为快照!
解决方案
您应该首先尝试编写连接 pg 并运行查询的独立节点应用程序。然后,当您知道连接数据库按预期工作时,您可以开始与应用程序的其他部分集成。现在这个问题有太多不相关的信息。
首先尝试在不使用 UNIX 套接字但使用 TCP 的情况下从 shell 连接 SQL 服务器:
psql postgres://postgres@localhost/smartbrain1
如果失败,则可能意味着您的数据库已配置为不允许任何外部 TCP 连接。
要允许从 localhost 访问 postgres,这应该在 pg_hba.conf 中通过设置
host all all 127.0.0.1/32 trust
此外,您可能需要为您的 postgres 用户添加密码并尝试启用密码连接:
psql postgres://postgres:<password>@localhost/smartbrain1
从命令行连接时,您可以在 knex 配置中尝试这样的操作:
const db = knex({
client: 'pg',
connection: 'postgres://postgres:<password>@localhost/smartbrain1'
});
在这里可以找到更多用于调试的信息Knex:Error Pool2 - error: password authentication failed for user和可能在数十个其他通用 postgres 数据库连接问题中。
推荐阅读
- c++ - 如何加速 OpenCV createMergeMertens 功能?
- coq - 如何判断依赖匹配两个 nat 相等
- firebase - 在显示警告的 Firebase 函数中,根据 GCLOUD_PROJECT 估计 Firebase 配置。初始化 firebase-admin 可能会失败 - 错误消息
- mysql - 通过交互表列表查看单个表
- typescript - 打字稿代码中的 T[number] 是什么意思?
- python - 库中的一些函数在 python shell 中工作,但不在脚本中
- node.js - 在启动时调试 dockerized nodejs 应用程序
- python - matplotlib 中每个图的宽度和高度轴相同
- c++ - 递归返回可被给定整数 k 整除的位数
- python - Visual Studio 中的 PyGame - 加载图像会出错,但是当我将相同的代码粘贴到 IDLE 时,它可以完美运行。有什么修复吗?