Reputation: 255
I have setup the node backend and try to connect to the local pgAdmin. When i try to run the Node app.js it's always shows the following error.
Error: connect ECONNREFUSED 127.0.0.1:5400
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5400
}
following is my code.
import('express').then((express) => {
import('got').then((g) => {
import('pg').then((p) => {
var cors = require('cors');
const got = g.default
const pg = p.default
const app = express.default()
app.use(cors())
const rtr = express.Router()
const masterRouter = express.Router()
const colorRouter = express.Router()
const bomRouter = express.Router()
const userRouter = express.Router()
const cdtRouter = express.Router()
const historyRouter = express.Router()
const { Pool } = pg
const baseUrl = 'https://xxxx.com/csi-requesthandler/api/v2'
const login = `${baseUrl}/session`
const customers = `${baseUrl}/customers`
const suppliers = `${baseUrl}/suppliers`
const styles = `${baseUrl}/styles`
const color_ways = `${baseUrl}/colorways`
const materials = `${baseUrl}/materials`
const boms = `${baseUrl}/apparel_boms`
const bom_revs = `${baseUrl}/apparel_bom_revisions`
const part_materials = `${baseUrl}/part_materials`
const db_user = 'admin'
const db_password = 'admin'
const db_host = 'localhost'
const db_catalog = 'postgres'
const db_port = '5400'
if (!db_user || !db_password || !db_host || !db_catalog) {
console.error('Database configuration params are missing from environment!')
process.exit(-1)
}
const pool = new Pool({
user: db_user,
host: db_host,
database: db_catalog,
password: db_password,
port: db_port
})
/**
*
* @param {*} req
* @param {*} res
* @param {()} next
* @returns call to next
*/
function tokenValidator(req, res, next) {
if (!req.headers.token) {
return res.status(400).json({ error: "Token must required" })
}
req.tokenCookie = req.headers.token
next()
}
rtr.use(express.json({ limit: '50mb' }))
rtr.use('/master', tokenValidator)
rtr.use('/master', masterRouter)
rtr.use('/color', tokenValidator)
rtr.use('/color', colorRouter)
rtr.use('/bom', tokenValidator)
rtr.use('/bom', bomRouter)
rtr.use('/user', tokenValidator)
rtr.use('/user', userRouter)
rtr.use('/cdt', cdtRouter)
rtr.use('/history', historyRouter)
app.use('/api', rtr)
app.listen(PORT, () => {
console.log(`Server is Listening of port ${PORT}`)
})
const cdt_map = []
rtr.get('/connection', (req, res) => {
return res.status(200).json({ success: 'Api Connected' });
})
rtr.post('/login', (req, res) => {
const rbody = req.body;
if (!rbody.username) {
return res.status(400).json({ error: 'Username not specified' })
}
if (!rbody.password) {
return res.status(400).json({ error: 'Password not specified' })
}
pool.query(`SELECT users.id, roles.id AS role, roles.role AS role_desc FROM users INNER JOIN roles ON roles.id = users.role WHERE LOWER(users.username) = LOWER('${rbody.username.toLowerCase().trim()}')`, (err, dbr) => {
if (err) {
return res.status(500).json({ error: 'Unable to query existance of the user', db: err })
}
if (dbr.rowCount == 0) {
return res.status(401).json({ error: 'User does not exist!' })
}
const uid = dbr.rows[0].id
const role = dbr.rows[0].role
const role_desc = dbr.rows[0].role_desc
got.post(login, { json: rbody })
.then((success) => {
const respBody = JSON.parse(success.body)
const cookie = { cookie: respBody.token, user_id: uid, type: role, desc: role_desc }
return res.contentType('application/json').send(cookie)
}, reject => {
if (reject.response.statusCode == 400) {
return res.status(400).json({ "error": "Invalid username or password" })
}
})
.catch((err) => { console.error(err) })
})
})
I can't figure out what's the error here. my locally run pgAdmin url is http://localhost:5432. i have checked and tried so many methods and still couldn't figure out the error. if anyone can help me out would be really appreciated.
Upvotes: 0
Views: 230
Reputation: 29
Usually postgres database listens on the default port: 5432.
Change your db_port
to 5432
and restart the node server.
Here is how to check which port is being used by postgres database.
sudo netstat -plunt |grep postgres
or if you able to run psql then run this command
\conninfo
or If you able to connect to pgAdmin (FYI pgAdmin runs on a different server on a different port from postgres database server) get the port from the server properties.
Upvotes: 1