Issue in connecting to heroku postgres from node + express + also locally - node.js

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');
});

Well, my node version was for some reason v14, not sure how that happened, but the most stable version in node site is 12, so I installed v12 and the connection to pg worked locally and remotely on heroku.
This is just to highlight what worked with me after trying 4 days straight.
However, that may trigger for you different issue like like this which I'm facing:
DeprecationWarning: Implicit disabling of certificate verification is deprecated and will be removed in pg 8. Specify `rejectUnauthorized: true` to require a valid CA or `rejectUnauthorized: false` to explicitly opt out of MITM protection.
All answers found so far point to: pg module already fixed in v7.18.1 but for some reason I can't force package.json to take that version, it jumps me to version 7.18.2
Tried that along with latest version 8.3 same issue with heroku, but locally the message doesn't show
Not big deal though, connection works for now until figuring it out.

I think the issue here is that you don't send back any response in the /students route .Notice the / route u have a res.json which sends back a response but in /students route i don't see where your response is sent and that's why you wait forever

Related

Heroku postgres timeout and SSL issues on API calls with Node

I'm trying to put my REST API built in Node and with PostgresSQL to Heroku. So, I created my application to Heroku, and created his own database. At this point, I tryied to commit, and the build worked corretly. This until I tryied to make some calls to the API. If the api calls I do has the uncorrect method, or doesn't exists, it gives me the correct error, but when the call is correct, there is a 503 error, with code H12, and description that is timeout error. Here is the code of one of the calls to the database that I'm testing:
router.get('/allpoints', async (req,res) =>{
try {
const points = await pool.query(
`SELECT nome, latitudine,longitudine
FROM luogo`);
res.json(points.rows);
}catch(err){
console.error(err.message);
}
});
Here there are the information about how I connect to the database.
const pool = new Pool({
connectionString: process.env.DATABASE_URL || 'postgresql://postgres:psw#localhost:5432/campione',
ssl: process.env.DATABASE_URL ? true : false
})
module.exports = pool;
The build on Heroku seems to work properly.
I read this question: Heroku h12 Timeout Error with PG / Node.js
It says that you have to put res.end() where there is not res.json(), but here there is the res.json(). So, I thought that the issue could be that there is an error that the route manage, and can't give back anything. So, I changed from console.log(err) to res.json(err), and the API response with `ssl self signed, as an error. At this point, in the second file, I put ssl as false by default, but it gaves me error because there is no SSL. I searched for a really long time for a solution, but I have not been able yet to fix the issue.
Someone thinks he knows what should I change? Thank you in advice
this option in databse config maybe useful
ssl: {
rejectUnauthorized : false,
}

local Postgres connection not returning anything

I’m trying to perform a simple query on a local database. I expect this query to return the schema names of the database.
I am running Postgres version 13.1 and I installed it by following the steps shown here: https://postgresapp.com/
As per guidelines on Postgres Wiki, I'm including config file changes, I only manually edited settings to enable logging.
This computer is running MacOS Big Sur Version 11.0.1.
I'm using Node.js and Postgres is running on port 5432 and I can access it with psql.
The relevant changes I've made are the following:
Endpoint in server.js:
router.post('/mock_call', async (ctx) => {
try {
console.log('sup')
await sql.mockCall()
ctx.body = {
status: "It's good",
data: 'good'
}
} catch (e) {
console.log(e)
ctx.body = {
status: "Failed",
data: e
}
ctx.res.statusCode = 422;
}
})
SQL File:
require("openssl")
const { Pool, Client } = require('pg');
const client = new Client({
user: 'user1',
host: 'localhost',
database: 'postgres',
password: 'mypass',
port: 5432,
});
module.exports = {
mockCall: function () {
console.log('mockCall begin')
client.connect(err => {
if (err) {
console.error('connection error', err.stack)
} else {
console.log('connected')
}
})
console.log('before query')
client.query("SELECT schema_name FROM information_schema.schemata", (err, res) => {
if (err) {
console.log('theres an error:')
console.log(err)
};
console.log('theres a response:')
console.log(res)
for (let row of res.rows) {
console.log(JSON.stringify(row));
}
client.end();
});
}
}
Logs that actually get printed out when I hit the endpoint on localhost:
sup
mockCall begin
before query
Postgresql Logs (not helpful it's as if the server never gets hit):
This exact project and code is working on my personal local computer and the query goes through as expected. It used to be working on a Heroku server I had set up. The only difference with the Heroku server is that the connection is made like so:
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
This connection had been working on a server I had for over a year. My database was running out of space so I upgraded from a hobby database to a standard plan on Heroku, the app continued to work. A couple weeks after this upgrade I pushed a new commit which included a couple new features on the app and this broke the postgresql connection. After this push I immediately checked out my last commit which was working and pushed that one, the issue however was still there.
I currently have the program running on my personal local computer but I need to move it back to Heroku as quickly as possible. The pictures and logs I've included above are the result of running my app locally on my friends computer, which seems to be having the same issue I'm having on Heroku so I'm hoping if I figure out the issue on his local computer I'll be able to solve what's going on in Heroku.
These are the logs that are printed out from my personal local computer which is working:
Edits:
Running psql -d postgres -U user1 -h localhost -p 5432 successfully connects me to the database on the command line.
The new features I added was a new endpoint for my apps customers. This commit works fine on my personal local computer, so I don't think it's an issue with the new features that I added. Additionally, since then I've reverted to my previous commit which used to be working so none of that new code is present anymore.
I'm running the entire app locally on my friends computer. I set up Postgres from scratch just as I did a year ago on my computer. However now, only my personal local computer is working.
I haven't changed anything on pg_hba.conf on either setup. This is what they both look like:
At first I thought the problem would be with Heroku since my local app was working fine. However after reaching out and talking for a couple days with support they said:
Hi there,
It looks like your application is able to successfully connect to the database, but something else in the application or framework is preventing the data from being retrieved. Unfortunately, as this is an application issue it falls outside the nature of the Heroku Support policy. I recommend searching our Knowledge Base or asking the community on Stack Overflow for more answers.
Turns out I was using an old version of pg, 7.8. I upgraded to 8.5 and now it works.

Frontend to backend request seems to be wrong

Hi I am trying to deploy a simple React + Node app to a remote server. Backend started with pm2 and seems ok, frontend works with nginx, but when I try to get data from the db, console sends message
GET http://localhost:8080/v1/names net :: ERR_CONNECTION_REFUSED xhr.js: 177
Not too sure if I need to replace request url with the server IP instead of localhost.
Any help is greatly appreciated.
Yes, you should change all your request from localhost to your deployed address, I would also propose you take a look at .env Files to do it. Node.js Everywhere with Environment Variables!.
usually, you should use .env files to automatically choose the good address when deployed and when coding locally. have a nice day.
example of code with a .env file from the back with mongoose.
mongoose
.connect(
`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}#cluster0.qvs4c.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`
)
.then(() => {
app.set("port", process.env.PORT || 5000);
app.listen(app.get("port"), function () {
console.log("Node app is running on port", app.get("port"));
});
})
.catch((err) => {
console.log(err);
});
example of my .env File.
DB_USER=thatsmydbuser
DB_PASSWORD=thatsmypassword123!
DB_NAME=thatsmydbname

heroku app crashes with socket.io

The application, an API server meant to communicate with a Unity client was working just fine, until I added sockets. Manually testing socket.io using the chrome extension socket.io tester demonstrated that it is working just great on my local machine. Now that I've deployed it to heroku, it doesn't work. It crashes right off.
Typically, answers to this issue are all about the PORT not being set correctly, but in this case I have set the port to process.env.PORT, so there shouldn't be any problems with that. My app was working just fine until I added socket.io.
Here is my basic server file:
import express from "express"
import mongoose from "mongoose"
import http from "http"
import config from "./config/index.js"
mongoose.Promise = global.Promise
// mongo
mongoose.connect(config.mongo.uri)
mongoose.connection.on("error", function(err) {
console.error("MongoDB services error: " + err)
})
// server
let app = express()
let server = http.createServer(app)
let socketio = require("socket.io")(server)
require("./socketio").default(socketio)
require("./express").default(app)
require("./api").default(app)
// start
server.listen(config.port, function () {
console.log("Listening on port: " + config.port)
})
export default app
Is there anything here to imply why the app may be crashing?
So for anyone who might also be having trouble with Heroku in this way. It turns out that I had my config file with the proper port setting listed in my .gitignore file. (I don't know how it got there!) I wasn't paying attention to the logs when the app was starting up so I was missing the log that it the config file was missing. I was impressed by how Heroku's staff was able to point that out.

Disabling Meteor 2 minute server timeout?

How can one remove/increase the server side connection timeout from Meteor when using server side routes with Iron Router?
There is a fix when using vanilla Node: https://contourline.wordpress.com/2011/03/30/preventing-server-timeout-in-node-js/
But I cannot figure out where to put the code in the above sample to make it work with Meteor and Iron Router
Here's a sample route:
Router.route('veryslowroute', {
path: '/veryslow',
where: 'server',
action: function () {
// Route never rendered in browser. Reducing value here to 110000 will render just fine
Meteor._sleepForMs(120000);
// These I've tried:
// this.response.setTimeout(0);
// this.response.connection.setTimeout(0);
// this.response.connection.server.setTimeout(0);
this.response.writeHead(200, {'Content-Type':'application/json'});
this.response.end(JSON.stringify({
key: 'Sorry for being so slow'
}));
}
});
So it appears the timeout needs to be set somewhere else. Anybody?
EDIT: It appears that this isn't about NodeJS but Meteor itself, likely the webapp-package. Still couldn't find a workaround. Same thing happens when using webapp directly without Iron Router.
EDIT: Now there is an issue in Meteor: https://github.com/meteor/meteor/issues/3826

Resources