- New to Node here, trying for the last 3 days straight, no clue.
- Read all similar issues and tried literally everything I could find, no luck.
- Suspecting something that is not common or related to my machine or code.
Issue: trying to fetch data in node.js from postgres db - to console (at least) so to render it later as HTML
Database has table name: students on heroku, and has records
Also locally on my macOS, I have postgres installed with simple data in a table called students
I couldn't fetch any data, no error, no idea how to track it!
Tried creating connection with pool, client.. also used heroku guide here exactly
Literally everything that other users mostly encountered
DATABASE_URL environment variable is ok, if i echo $DATABASE_URL in Terminal:
postgres://xnlkikdztxosk:kuadf76d555dfab0a6c159b8404e2ac254f581639c09079baae4752a7b64a@ec3-52-120-48-116.compute-1.amazonaws.com:5432/uytnmb7fvbg1764
- When i run 'node app.js' server starts ok on port 3000, I can use postman on the root '/' OK and it works, it returns back the json info and console.log
- If i try postman to '/students' then it tries forever, no results, no error, nothing
- Tried also with my local installation of postgres, same thing
- My modules are ok, and I run npm install several times
- Thought could be my mac firewall, i turned it off completely
- Also tried this, nothing prints out or no idea where to track it:
process.on('uncaughtException', function (err) {
console.log(err);
});
Guide or steps to follow in order to track issues like this will be highly appreciated
app.js file: const express = require('express') const bodyParser = require('body-parser') const { Client } = require('pg'); const app = express() const PORT = 3000 app.use(bodyParser.json()) app.use( bodyParser.urlencoded({ extended: true, }) ) const client = new Client({ connectionString: process.env.DATABASE_URL, ssl: { rejectUnauthorized: false } }); client.connect(); app.get('/', (req, res) => { res.json({ info: 'Info: This is the root directory' }); console.log('main directory') }) app.get('/students', (req, res) => { client.query('SELECT * FROM students;', (err, res) => { if (err) throw err; for (let row of res.rows) { console.log(JSON.stringify(row)); console.log('WHOOOOOO, finally!'); } client.end(); }); }); app.listen(PORT, function(){ console.log('Server running on port 3000'); });