Introducci贸n a Node.js y Express
Node.js y Express
Entorno de trabajo
Variables de entorno
Rutas din谩micas
Body parser
Postman
CRUD
驴Que es una API Restful?
Solicitudes GET
Solicitudes POST
CRUD
Soluci贸n del reto de validaci贸n
Solicitudes DELETE
MIddlewares
Middlewares - logger
Middlewares - ErrorHandler
DB
Instalar Postgresql
Instalar Prisma y PostgreSQL Client
Prisma Models
JWT
Autenticaci贸n utilizando JSON Web Tokens (JWT)
Auth - Register
Auth - Login
Expres.js
Arquitectura
Arquitectura parte 2
Creaci贸n y Migraci贸n de Modelos con Prisma para Citas M茅dicas
Admin controllers / services
Admin controllers / services parte 2
Reservations
Reservations parte 2
Appointments
Deploy
PostgreSQL
Deploy
You don't have access to this class
Keep learning! Join and start boosting your career
Environment variables are fundamental in Node.js application development, allowing us to configure different aspects of our application depending on the environment in which it runs. In this article, we will explore how to implement and manage environment variables in a Node.js application, as well as some best practices to optimize our development workflow.
When we develop applications in Node.js, it is common to need different configurations depending on the environment (development, test, production). Environment variables allow us to manage these configurations efficiently.
In our example, we are configuring the port on which our application will run:
require('dotenv').config();
const port = process.env.PORT || 3000;
console.log(port);
// Rest of the application code.
It is important to note that some variables will have default values (as in the case of the port that uses 3000 if no other is specified), while others should not have default values in order not to lose their purpose.
In order for our application to be able to read the variables defined in an .env
file, we need to install the dotenv
package:
npm install dotenv
Once installed, we must import it and configure it at the start of our application:
require('dotenv').config();
This package automatically loads the variables defined in the .env file and makes them available through the process.env
object.
When working in development, it is common to make frequent changes to our code. To facilitate this process, we can configure scripts in our package.json
file.
{ " scripts": { " dev": "node --watch app.js", " start": "node app.js" }}
This configuration allows us to:
npm run dev
.npm start
These scripts simplify repetitive commands and standardize the way to run the application in different environments.
It is essential to understand that:
To improve the development experience, we can add informative messages to facilitate the access to our application:
app.listen(port, () => { console.log(`Serverrunning at http://localhost:${port}`);});
This message in the console allows us to:
These small improvements make the development process smoother and less error prone.
Environment variables are a powerful tool for managing configurations in different environments. Implementing them correctly, along with good development practices such as custom scripting, significantly improves our workflow. We encourage you to experiment with these techniques in your projects and share your experiences in the comments section.
Contributions 4
Questions 2
Want to see more contributions, questions and answers from the community?