I uploaded my node/express server to Heroku . Also on Heroku I have a postgres database to which I am connected with pgAdmin 4 locally. Since pgAdmin 4 when I write the query for example SELECT * FROM users , it returns the response, but when I use Insomnia to make the request to Heroku it returns a status 500 with the message "Internal server error" .
Obviously there is an error but I don't know where it could be, below I show the configurations of my files.
So I have the folders :
Here users.controller with the getUsers() function , where it should return all users.
import { Request, Response } from 'express'
import { QueryResult } from 'pg'
import { pool } from '../database'
export const getUsers = async (req: Request, res: Response): Promise<Response> => {
try {
const response: QueryResult = await pool.query('SELECT * from users order by username asc');
return res.status(200).json(response.rows);
}
catch (e) {
console.log(e);
return res.status(500).json('Internal server error');
}
}
Here is the route in user.routes.ts
import { Router } from 'express'
const router = Router();
import { getUsers } from '../controllers/user.controller'
router.get('/users', getUsers); // get todos los usuarios
export default router;
Here is the database.ts file , with the data that Heroku gave me to make the connection.
import { Pool } from 'pg'
export const pool = new Pool({
user: 'datos de heroku',
host: 'datos de heroku',
password: 'datos de heroku',
database: 'datos de heroku',
port: 5432
})
Here is the complete package.json , in case there is something missing:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"dev": "concurrently \"tsc --watch\" \"nodemon dist/server.js\"",
"build": "tsc",
"start": "node dist/server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.8",
"@types/jsonwebtoken": "^8.5.0",
"@types/pg": "^7.14.5",
"concurrently": "^5.3.0",
"nodemon": "^2.0.4",
"typescript": "^4.0.3"
},
"dependencies": {
"bcrypt": "^5.0.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mercadopago": "^1.5.5",
"multer": "^1.4.2",
"pg": "^8.4.0",
"stripe": "^8.126.0",
"uuid": "^8.3.1"
},
"engines": {
"node": "14.x"
}
}
here the server file
import dotenv from 'dotenv'
dotenv.config(); // aca lee las variables de entorno para el token
import express from 'express'
var multer = require('multer')
const uuid = require('uuid')
const app = express(); // inicializo express
const cors = require('cors');
const path = require('path');
//rutas
import userRoutes from './routes/user.routes'
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(userRoutes);
app.use(express.static('public')); // Carpeta public la hago de acceso publica. para poder ver las imagenes desde el navegador
// Configuro el puerto. Tomo el puerto del sistema operativo o el 3000
app.set('port', process.env.PORT || 3000);
// Inicio el servidor
app.listen(app.get('port'), () => {
console.log('usuarios server on port:', app.get('port'));
});
The Heroku view logs were showing this:
Apparently Heroku Postgres uses self-signed certificates for security, this prevented me from accessing the database. The solution was to disable SSL certificate verification by adding this line ssl: { "rejectUnauthorized": false } to my database.ts file