<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>REST API on Israel Brea Piñero</title>
        <link>https://israelbreap-portfolio.pages.dev/tags/rest-api/</link>
        <description>Recent content in REST API on Israel Brea Piñero</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en</language>
        <lastBuildDate>Mon, 10 Mar 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://israelbreap-portfolio.pages.dev/tags/rest-api/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>Creating an API with Express and deploying to the cloud with AWS</title>
        <link>https://israelbreap-portfolio.pages.dev/p/api-express-aws/</link>
        <pubDate>Mon, 10 Mar 2025 00:00:00 +0000</pubDate>
        
        <guid>https://israelbreap-portfolio.pages.dev/p/api-express-aws/</guid>
        <description>&lt;img src="https://israelbreap-portfolio.pages.dev/p/api-express-aws/apicloud.png" alt="Featured image of post Creating an API with Express and deploying to the cloud with AWS" /&gt;&lt;p&gt;This project was developed as part of the subject Server-Side Technologies: Cloud Computing within the Master&amp;rsquo;s Degree in Mobile Computing (MIMO). The objective was to implement a complete REST API using &lt;strong&gt;Node.js and Express&lt;/strong&gt;, with authentication using &lt;strong&gt;JWT&lt;/strong&gt;, persistence in &lt;strong&gt;SQLite&lt;/strong&gt;, and deployment on &lt;strong&gt;AWS&lt;/strong&gt; following good scalability and automation practices.&lt;/p&gt;
&lt;h2 id=&#34;project-objectives&#34;&gt;Project objectives
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Implement a REST API according to an &lt;strong&gt;OpenAPI&lt;/strong&gt; specification.&lt;/li&gt;
&lt;li&gt;Manage authentication and authorization with &lt;strong&gt;JWT&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Implement &lt;strong&gt;CRUD&lt;/strong&gt; operations for movie and rating resources.&lt;/li&gt;
&lt;li&gt;Apply data validations and proper handling of HTTP status codes.&lt;/li&gt;
&lt;li&gt;Deploy the API on &lt;strong&gt;AWS&lt;/strong&gt; with a scalable and automated infrastructure using &lt;strong&gt;Packer&lt;/strong&gt; and &lt;strong&gt;Terraform&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;main-functionalities&#34;&gt;Main functionalities
&lt;/h2&gt;&lt;h3 id=&#34;1-authentication-and-security&#34;&gt;1. Authentication and security
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Login endpoint&lt;/strong&gt; (&lt;code&gt;POST /sessions&lt;/code&gt;) to generate JWT tokens.&lt;/li&gt;
&lt;li&gt;Protect sensitive routes using &lt;strong&gt;authentication middleware&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Handling authentication errors with appropriate responses (&lt;code&gt;401 Unauthorized&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;2-movie-management-movies&#34;&gt;2. Movie management (&lt;code&gt;/movies&lt;/code&gt;)
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;GET /movies&lt;/strong&gt;: Returns all movies with required fields (ID, title, genre, duration, rating).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optional pagination&lt;/strong&gt; to improve efficiency in large queries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error handling&lt;/strong&gt; with appropriate responses (&lt;code&gt;404 Not Found&lt;/code&gt;, &lt;code&gt;500 Internal Server Error&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;3-rating-management-ratings&#34;&gt;3. Rating management (&lt;code&gt;/ratings&lt;/code&gt;)
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Full CRUD&lt;/strong&gt; so users can create, read, update, and delete ratings.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Validations&lt;/strong&gt;:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rating&lt;/code&gt; must be between &lt;strong&gt;0 and 5&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;comments&lt;/code&gt; with a maximum of &lt;strong&gt;500 characters&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Access restrictions&lt;/strong&gt;:&lt;/li&gt;
&lt;li&gt;Only authenticated users can modify or delete their ratings.&lt;/li&gt;
&lt;li&gt;Error handling with appropriate codes (&lt;code&gt;401 Unauthorized&lt;/code&gt;, &lt;code&gt;422 Unprocessable Entity&lt;/code&gt;, &lt;code&gt;201 Created&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;4-watchlist-management-watchlist&#34;&gt;4. Watchlist management (&lt;code&gt;/watchlist&lt;/code&gt;)
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;Allows users to add and manage movies to watch.&lt;/li&gt;
&lt;li&gt;Validation of &lt;strong&gt;Movie IDs&lt;/strong&gt; before adding them.&lt;/li&gt;
&lt;li&gt;Handling of watched/unwatched movie statuses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Appropriate responses&lt;/strong&gt;:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;409 Conflict&lt;/code&gt; for duplicate movies.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;422 Unprocessable Entity&lt;/code&gt; for invalid IDs.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;404 Not Found&lt;/code&gt; for non-existent movies.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;technical-implementation&#34;&gt;Technical implementation
&lt;/h2&gt;&lt;h3 id=&#34;technologies-used&#34;&gt;&lt;strong&gt;Technologies used&lt;/strong&gt;
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Node.js + Express&lt;/strong&gt;: Development of the REST API.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SQLite&lt;/strong&gt;: Lightweight and efficient database.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;JWT (jsonwebtoken)&lt;/strong&gt;: Authentication implementation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Docker&lt;/strong&gt;: Containerization of the development environment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;AWS (EC2, S3, IAM)&lt;/strong&gt;: Cloud infrastructure.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Packer&lt;/strong&gt;: Creation of AMI images for AWS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Terraform&lt;/strong&gt;: Automation of cloud deployment.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;persistence-and-database&#34;&gt;&lt;strong&gt;Persistence and database&lt;/strong&gt;
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;Use of &lt;strong&gt;SQLite&lt;/strong&gt; to store user, movie, and rating information.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Migrations and defined schema&lt;/strong&gt; to ensure data integrity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ORM Sequelize&lt;/strong&gt; to facilitate database management.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;deployment-on-aws&#34;&gt;&lt;strong&gt;Deployment on AWS&lt;/strong&gt;
&lt;/h3&gt;&lt;h4 id=&#34;1-creating-ami-with-packer&#34;&gt;&lt;strong&gt;1. Creating AMI with Packer&lt;/strong&gt;
&lt;/h4&gt;&lt;p&gt;An &lt;strong&gt;Amazon Machine Image (AMI)&lt;/strong&gt; was generated with the necessary configuration to run the API:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Installation of &lt;strong&gt;Node.js&lt;/strong&gt; and dependencies.&lt;/li&gt;
&lt;li&gt;Configuration of the environment and necessary variables.&lt;/li&gt;
&lt;li&gt;Creation of a script for automatic execution of the service.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id=&#34;2-infrastructure-with-terraform&#34;&gt;&lt;strong&gt;2. Infrastructure with Terraform&lt;/strong&gt;
&lt;/h4&gt;&lt;p&gt;The infrastructure was implemented with &lt;strong&gt;Terraform&lt;/strong&gt;, ensuring scalability and automation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;EC2 instance&lt;/strong&gt; configured with the generated AMI.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Load balancer&lt;/strong&gt; to distribute traffic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto Scaling Group&lt;/strong&gt; to increase or decrease instances on demand.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RDS storage&lt;/strong&gt; for data persistence in the cloud.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IAM role and permission management&lt;/strong&gt; to ensure security.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;automation-and-scalability&#34;&gt;&lt;strong&gt;Automation and scalability&lt;/strong&gt;
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Autoscaling infrastructure&lt;/strong&gt; that allows adjusting the number of instances based on load.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error handling&lt;/strong&gt; to ensure high availability and minimize downtime.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data persistence&lt;/strong&gt; ensured with storage in AWS.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;conclusions-and-learnings&#34;&gt;Conclusions and learnings
&lt;/h2&gt;&lt;p&gt;This project allowed us to consolidate knowledge in &lt;strong&gt;backend development with Express&lt;/strong&gt;, JWT authentication, &lt;strong&gt;SQL&lt;/strong&gt; databases, as well as in &lt;strong&gt;cloud infrastructure and deployment automation&lt;/strong&gt; in AWS. Good security practices, data validations and error handling were applied, achieving a robust and scalable system.&lt;/p&gt;
&lt;h2 id=&#34;resources-and-source-code&#34;&gt;Resources and source code
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://github.com/israelbrea12/Despliegue-API-express-AWS.git&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;GitHub repository&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;../../mimo_movies.yaml&#34; &gt;OpenAPI documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        </item>
        
    </channel>
</rss>
