You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
5 Hrs
19 Min
57 Seg
Curso de Backend con ExpressJS

Curso de Backend con ExpressJS

Oscar Barajas Tavares

Oscar Barajas Tavares

Deploy

30/30
Resources

Implementing a robust API in Express is a crucial step for any developer looking to create scalable and secure web applications. Deploying your API along with a cloud database allows you to have a fully functional system accessible from anywhere. This process, although technical, becomes simple when you follow the right steps and understand the fundamental concepts.

How to deploy your Express API in the cloud?

Once you have validated that your database is properly deployed and the connection is working properly, it is time to take your API into production. This process involves using cloud services that make it easy to deploy and manage web applications.

Preparing the repository for deployment

Before starting the deployment process, it is critical that you upload all your changes to a Git repository, either private or public. This step is crucial because:

  • It allows the deployment service to access your code
  • Facilitates version management
  • Enables continuous integration if you decide to implement it.

To start the deployment, you must create a new resource on the selected platform (in this case, Railway) and connect it to your repository:

  1. Select the option to create a new resource
  2. Choose the option to connect to a repository
  3. Select the repository that contains your Express code

Setting environment variables

Environment variables are essential for keeping your application's sensitive data secure. During the deployment process, you must configure:

  • DATABASE_URL: The connection string to your database
  • JWT_SECRET: A secret key for JWT token generation and validation.
// Example of how these variables are used in the codeconst jwt = require('jsonwebtoken');const token = jwt.sign({ userId: user.id }, process.env.JWT_SECRET);

It is important that for the JWT_SECRET you use a complex phrase and not a simple word, as this increases the security of your authentication system.

Construction and deployment process

Once you have configured the environment variables, you can proceed with the deployment:

  1. The system will automatically detect that it is an Express project.
  2. It will start the building process
  3. Deploy the application to an accessible URL

This process may take a few minutes the first time, but subsequent deployments are usually faster. During the process, you can check the logs to identify possible errors or confirm that everything is working correctly.

How to test your deployed API?

Once your API is deployed, it is essential to verify that all endpoints are working correctly. For this, you can use tools such as Postman.

Updating the URLs in your tests

The first step is to update the URLs in your test requests:

  1. Replace localhost:5000 with the URL provided by the deployment service.
  2. Keep the same paths you had in development
// Before (local development)POST http://localhost:5000/api/auth/register
// After (production)POST https://tu-dominio-asignado.railway.app/api/auth/register

Registration and authentication test

To verify that your authentication system is working properly:

  1. Create a new user via the registration endpoint.

    • Send a JSON with the user's data (name, email, password)
    • Verify that you receive a successful response
  2. Test the login with the credentials of the created user

    • Send the email and password to the login endpoint
    • Confirm that you receive a valid JWT token
  3. Verify in the database that the user has been created correctly

    • Accesses your cloud database
    • Verify that the user data is stored correctly

It is important to remember that passwords must be encrypted in the database, never in plain text.

What challenges can you face to improve your API?

Implementing a functional API is just the beginning. To take it to a professional level, you can face several challenges that will improve both your skills and the quality of your service.

RFC (Request for Comments) Implementation

An RFC is a proposal to improve or extend the functionality of your API. Some challenges you can address include:

  • Improving the medical appointment model:

    • Adding time ranges for appointments.
    • Include assigned physician information
    • Relate appointments to specific users
  • Expand the role system:

    • Implement more granular permissions
    • Create additional roles besides administrator and normal user
  • Optimize error handling:

    • Create more descriptive error responses
    • Implement a centralized logging system

Application of acquired knowledge

During the course you have learned fundamental concepts that you can continue to apply:

  • Middlewares for log and error handling
  • Authentication systems with encrypted passwords
  • Protected routes based on user roles
  • Full CRUD for different entities (users, appointments, etc.)

This knowledge allows you to build robust APIs for any type of application, from management systems to chatbots for WhatsApp.

Developing APIs with Express opens up a world of possibilities for creating modern and efficient web applications. Keep practicing, implementing new features and, above all, never stop learning.

Contributions 0

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?