Hey there,
Are you looking to connect to a PostgreSQL database. Here is a sample Nodejs function that can connect to a database remotely and provide scalable API endpoint to retrieve list of tables.
Here, the GET method is 'GET' and the endpoint is '/query'.
Make sure that you install express and postgres-pool packages in your advanced I/O function using "npm install --save <<package_name>>"
- var express = require('express');
- var app = express();
- const { Pool } =require('postgres-pool');
- const QUERY="SELECT * from information_schema.tables where table_schema = 'public'"
- const pool = new Pool({
- connectionString: 'postgres://user_name:password@127.0.0.1/db_name',
- });
- function getDBdata() {
- return new Promise((resolve, reject) => {
- pool.query(QUERY).then(queryResponse => {
- resolve(queryResponse.rows);
- }).catch(err => {
- reject(err);
- })
- });
- }
- app.get('/query', function (req, res) {
- getDBdata().then(
- rows => {
- var html = "<h1> List of Tables in DB</h1>";
- rows.forEach(element => {
- html = html.concat('<li>' + element.table_name + '</li><br/>');
- });
- res.send(html);
- }).catch(err => {
- console.log(err);
-
- })
- });
- module.exports = app;