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 ] }