gpt4 book ai didi

node.js - Sequelize 无法连接到 postgres 服务器

转载 作者:行者123 更新时间:2023-11-29 13:17:49 25 4
gpt4 key购买 nike

我正在尝试使用 Sequelize 连接到 postgres 数据库。当我尝试在数据库中创建一行时,出现以下错误消息。当我尝试连接的数据库 url 是端口 5432 时,我不确定为什么端口列出 3306。我尝试在配置文件中设置端口和主机名,但我仍然遇到相同的错误。

{
"name": "SequelizeConnectionRefusedError",
"message": "connect ECONNREFUSED 127.0.0.1:3306",
"parent": {
"code": "ECONNREFUSED",
"errno": "ECONNREFUSED",
"syscall": "connect",
"address": "127.0.0.1",
"port": 3306,
"fatal": true
},
"original": {
"code": "ECONNREFUSED",
"errno": "ECONNREFUSED",
"syscall": "connect",
"address": "127.0.0.1",
"port": 3306,
"fatal": true
}
}

我在我的 server.js 文件中运行我的应用程序:

const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const fs = require('fs')

const PORT = process.env.PORT || 3000


// INSTANTIATE EXPRESS APP
const app = express()

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json({
type: function() {
return true
}
}))

// SET VIEW ENGINE
app.use(express.static(path.join(__dirname, 'public')))

require('./src/server/routes')(app)

app.get('*', function(request, response) {
response.sendFile(path.resolve(__dirname, 'public', 'index.html'))
})

// SET FOLDER TO SERVER STATIC ASSETS
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT,DELETE');
res.setHeader(
"Access-Control-Allow-Headers",
"Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, username")
//and remove cacheing so we get the most recent comments
res.setHeader('Cache-Control', 'no-cache');
next();
});

// SERVER APP
app.listen(PORT, () => {
console.log('Server running on localhost:' + PORT)
})

我的配置文件设置为使用在线数据库

{
"development": {
"url": "postgres://database@stampy.db.elephantsql.com:5432/znjpptuy",
"dialect": "postgres",
"PORT": 5432,
"HOST": "127.0.0.1"
}
}

最佳答案

仔细检查后,意识到错误与我传递给新的 sequelize 实例的 url 有关,在本例中,是缺少 url。

sequelize = new Sequelize(process.env[config.url]);

process.env 一直返回为未定义,所以我最终将实际 url 传递给函数。

关于node.js - Sequelize 无法连接到 postgres 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46456999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com