0

I've made a simple JS file that hosts a local server, and then uses Node to read a db and return the results. My aim is to create a page that just returns the rows and then later on, can create new ones.

The issue is that the console logs the results, but the server doesn't connect. I can make files that just host servers, but putting in the query stops the server from running. How can I keep my query running on a server?

Here's the code:

    const pg = require("pg");
    const pool = new pg.Pool({
    user: "James",
    host: "127.0.0.1",
    database: "makersBnb",
    password: "",
    port: "5432"});

    pool.query('SELECT * FROM listings', (err, res) => {
    console.log(err, res);
    pool.end();
    });

The table has 2 rows. Here's the console:

Result {
  command: 'SELECT',
  rowCount: 2,
  oid: null,
  rows:
   [ { id: 2,
   listing_name: 'testListing',
   price: '5',

   description: 'description',
   owner_name: 'me',
   email: '[email protected]',
   phone_num: '07777777771' },
 { id: 1,
   listing_name: 'a',
   price: 'b',
   description: 'c',
   owner_name: 'd',
   email: 'e',
   phone_num: 'f' } ],


_parsers:
   [ [Function: parseInteger],
     [Function: noParse],
     [Function: noParse],
     [Function: noParse],
     [Function: noParse],
     [Function: noParse],
     [Function: noParse] ],
  RowCtor: null,
  rowAsArray: false,
  _getTypeParser: [Function: bound ] }

1 Answer 1

1

Hope that my project can give you some idea.

in postgreSQL file:

const {Pool} = require('pg');
const config = require('../config');
const postgre = config.postgre;
const pool = new Pool(postgre);
module.exports = {
    getList: (/* could add params here */ callback) => {
        let param = []; //if there is params add to here in sequence,
        // for text using let param = [email]
        let query = 'select * from listings;'
        // text is sample using params;
        let text = 'select * from users where email=$1';
        return pool.query(text, param, callback)
    },
    // here you can add more functions as you like.
}

In app.js

const db = require('/postgresql') //import the file abave
app.get('/getList',(req,res)=>{
    db.getList((err,messageFromDB)=>{
        if (err) {
            console.log(err);
        } else {
            res.send(messageFromDB.rows);
        }
    })
}

This is will make every time you call /getList and database (node.js server) return the result. or you can simply console.log the result or do something else.

UPDATE: when reviewed my answer and reviewed your question, you should not call pool.end(); if you want the server continues running.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.