Use connection Postgresql on NodeJS (express)

2018-10-26T11:33:25

I'm a novice at NodeJS and Express, I followed a tutorial to create a CRUD with a connection to Mysql where "express-myconnection" is used, that connection is recorded as middleware and is used from a model or controller. But I need to work with postgresql, I managed to make the connection, my problem is that I can not call it from a model or a controller

const express = require('express');
const path = require('path');
const morgan = require('morgan');
const app = express();
const { Client } = require('pg');
const connectionData = {
    user: 'postgres',
    host: 'localhost',
    database: 'bex',
    password: 'postgres',
    port: 5432,
}
const client = new Client(connectionData);
client.connect()
client.query('SELECT * FROM bex_usuario')
    .then(response => {
        console.log(response.rows)
        client.end()
    })
    .catch(err => {
        client.end()
    })

How would you create a middleware similar to "express-myconnection", or how would you instantiate from my model or controller without the need to define it in each model or controller?

Copyright License:
Author:「Wilmer」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/53001022/use-connection-postgresql-on-nodejs-express

About “Use connection Postgresql on NodeJS (express)” questions

I'm a novice at NodeJS and Express, I followed a tutorial to create a CRUD with a connection to Mysql where "express-myconnection" is used, that connection is recorded as middleware and is used fro...
I’m a long term programmer, but haven’t used nodejs much in my code. Now I need to use it in my current code and I’ve ran into a problem that I can’t seem to figure out myself, I have googled a lot...
I have the following postgresql connection file in nodejs: // postgresql.js "use strict"; const { Pool } = require('pg'); module.exports = new Promise((resolve, reject) => { const...
I created a course management system using PHP and HTML. I use PostgreSQL to store data. I use my school's server and database where I could put my files on there with WinSCP and can connect to the
I am trying to connect to my Heroku PostgreSQL DB and I keep getting an SSL error. Does anyone have an idea on how to enable SSL in the connection string? postgres://user:pass@host:port/database; ...
I see very few online posts when it comes to NodeJS with IBM DB2. I am new to NodeJS and having issues to configure connection pooling for my web app. I am successfully running node app with single
I am facing some analysis paralysis here. There are so many options for programming databases with NodeJS that I am a bit lost. I am building an API server using Express that will talk to a HTML5 ...
A couple of weeks ago I started a blog app. For this I used postgresql as database on the backend. Back then, the application worked fine I was able to fetch data from the frontend to the backend s...
i'm newbie in nodejs and i want to post and get data to nodejs server and have realtime inside application and server. in this code my code is not correct. i'm using both of express and socket and i
I'm trying to set up some routes on the backend of an app I'm building and I keep getting the aforementioned error. I've made many backends with NodeJS and Express and never ran into this issue. The

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.