馃憦
Introducci贸n
C贸mo autenticar usuarios con NestJS
Instalaci贸n de PlatziStore con MongoDB
Instalaci贸n de PlatziStore con TypeORM
Protecci贸n con guardianes
Introducci贸n a Guards
Usando un decorador
Guard con variables de ambiente
Autenticaci贸n con Passport
Hashing de contrase帽as en TypeORM
Hashing de contrase帽as en MongoDB
Autenticaci贸n con Passport.js
Ruta de login
Autenticaci贸n con JSON Web Tokens
Conectando Passport con JWT
Secret desde variables de entorno
Implementando JWT Guard
Extendiendo JWT Guard
Control de roles en NestJS
Obteniendo 贸rdenes del perfil
Deployment
Configurando Mongo Atlas
Deploy de Mongo en Heroku
Configuraci贸n de PostgreSQL en Heroku
Deploy de Postgres en Heroku
Corriendo migraciones de Postgres en Heroku
Pr贸ximos pasos
驴Quieres m谩s cursos de NestJS?
You don't have access to this class
Keep learning! Join and start boosting your career
Starting a project in Heroku and connecting to a Postgres database may seem complicated at first, but with the correct configuration of environment variables, the process is simplified. Heroku provides an environment variable called DATABASE_URL that encapsulates all the data needed for the connection, eliminating the need to specify parameters such as user, password, host, and port individually.
In our project it is essential to make changes to be able to read the DATABASE_URL variable. Before, we had the connection very segmented:
Postgres
)Now, we must integrate these elements into a single URL in a standard format, both for development and production environments. In this context, the URL format would be something like:
postgres://user:password@host:port/database
Once the connection URL is obtained, we need to modify the project's database modules. Here, we will go from reading individual variables to only needing the connection URL. The steps would be as follows:
POSTGRES_URL
variable.POSTGRES_URL
variable would correspond to the value of DATABASE_URL
provided by Heroku.For example:
const databaseURL = process.env.POSTGRES_URL;// Use `databaseURL` for the connection to the DBMS.
Before proceeding with the deployment, some settings are necessary:
Additional environment variables: some settings, like DATABASE_NAME
, PORT
, or secrets like API_KEY
and JW_SECRET
, must be managed locally and in Heroku. Make sure these keys are not lost in production.
Validations: In the variable validation section, make sure to verify that DATABASE_URL
is present when running on Heroku.
SSL configuration: Enable the required SSL configuration. To do this, add the following in your connection settings:
ssl: { rejectUnauthorized: false }
This setting is critical for securing data transmissions in a production environment.
Once the deployment is done, it is crucial to verify that the connections are successful. This can be done by visiting the URL where the deployment was performed. Similarly, monitoring logs can provide valuable information for troubleshooting.
To track logs in real time, use the following command:
heroku logs --tail
In case of database-related errors, make sure you have run all the migrations that your database schema requires. This step is essential, and its lack can result in error messages indicating non-existent tables or records.
The correct configuration of environment variables and the adaptation of the project to use DATABASE_URL
significantly simplify the deployment process in Heroku. Keep the configuration tidy and systematically check the proper functioning with logs to combat possible errors. Keep going and don't stop exploring new ways to optimize and improve your Heroku workflow!
Contributions 2
Questions 3
馃憦
DATABASE_URL=postgres://root:123456@localhost:5432/my_db
Want to see more contributions, questions and answers from the community?