<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Leandro Merli on Medium]]></title>
        <description><![CDATA[Stories by Leandro Merli on Medium]]></description>
        <link>https://medium.com/@leandromerli?source=rss-b28813d75e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GpsTfI_-w_fO2R3TC917Lg.jpeg</url>
            <title>Stories by Leandro Merli on Medium</title>
            <link>https://medium.com/@leandromerli?source=rss-b28813d75e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 16:37:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leandromerli/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Elastic Beanstalk — Pros and Cons]]></title>
            <link>https://medium.com/@leandromerli/elastic-beanstalk-pros-and-cons-852e41fc74c?source=rss-b28813d75e------2</link>
            <guid isPermaLink="false">https://medium.com/p/852e41fc74c</guid>
            <category><![CDATA[elastic-beanstalk]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Leandro Merli]]></dc:creator>
            <pubDate>Thu, 06 Jan 2022 04:42:52 GMT</pubDate>
            <atom:updated>2022-01-06T04:42:52.188Z</atom:updated>
            <content:encoded><![CDATA[<h3>Elastic Beanstalk — Pros and Cons</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*eIkobwwAa2_rGfR-.png" /><figcaption>Multiple environments in a single AWS account</figcaption></figure><p>Elastic beanstalk is a service of Amazon Web Services that allows you to deploy an API or website from the command line and it automatically creates the resources to run it. It allows you to have multiple environments: dev, stage, prod and even create an environment just to test a feature (just by changing a line of code) and then terminate it easily.</p><p>Behind the scenes Elastic Beanstalk creates an EC2 instance to run your code (an others AWS resources)</p><p>You can set up Elastic and the environment with a simple setting page in AWS where you can configure the type of instance that it will create, the software to run (Node.js, Java, Python and others), the load balancer, security and many others.</p><p>Another cool thing about Elastic is that you can set the environment to be Auto Scalable. If your site is under high traffic it automatically creates new instances to handle it.</p><p>In the settings page you can set based on CPU usage, Memory, Latency, Disk I/O, Network and other Metric, the creation of new instances of your server. You can set a minimum and a maximum instances created.</p><p>If the traffic or server usage goes down it will destroy the instances created so you save money.</p><p>This work is done by a Load Balancer created automatically for you.</p><p>In my experience I found it a little difficult to know what metric to use and what value to set. I had to set a metric and then test how it responded in high traffic. But it is really difficult to see if everything goes well. You have to check logs and statistics under high pressure and adjust the values entered. You have multiple variations that can change everything.</p><p>One cool feature is when you deploy, it launches a new server instance so your site never goes offline. If it fails nothing will happen. You’ll still have your “old” instance running.</p><h3>Pros:</h3><ul><li>You can add a load balancer and make AWS create multiple instances when the traffic to your site or the amount of API calls is high.</li><li>You can deploy and the site will not go offline. AWS will create new instances with the new code and have others instances with the old code running while the new ones are created and ready to receive traffic.</li><li>You can easily deploy from the command line (locally) or with Jenkins server. That will deploy code and create the instances (like a pipelines).</li><li>You can have multiple environments (in differents domains or subdomains) so you can have a stage test site.. or you can even test different versions of your site.</li></ul><h3>Cons:</h3><p>You have to remember that if you are using a load balancer Elastic will create multiple instances or in a single instance setup that instance will be created and destroyed in every deployment. The best way is to check inside Elastic the indicators of what is going on: the logs, metrics, health, etc.</p><ul><li>You can use the local instance memory to save some cache or some values that you need to store, but remember: that memory is erased when you deploy a new code an new instances are created or in a multi instance site, some request can go to one instance and others request to the other (You might need to use some caching system like Amazon MemoryDB for Redis, Memcached or your database to store values).</li><li>You have to configure 1 instance with Autoscaling. Even if you have a small site</li></ul><p>Below some pics from my environments</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*opsUUU1kANfAGAjXlu-jCg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q0Fwmx-41R-lTkovFAi6Ew.png" /></figure><p>Autoscaling configuration</p><p>1–4 instances</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XCIUSR-UpyU0KNeGmWfnDQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SElUcAEYoVYz-QPSjLTIlg.png" /></figure><p>Single Instance without AutoScaling</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XfLXSj4gwzyjcz2LpGWA9g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sesJlT05EMUMKlBPAEFatQ.png" /></figure><p>Example versioning</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B41bhfpALlnxjq2cjUlzSQ.png" /></figure><p><em>Originally published at </em><a href="https://leandromerli.com/elastic-beanstalk/"><em>https://leandromerli.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=852e41fc74c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How do we replace Moment.js from our Angular Project]]></title>
            <link>https://medium.com/@leandromerli/how-do-we-replace-moment-js-from-our-angular-project-169834eaa1e8?source=rss-b28813d75e------2</link>
            <guid isPermaLink="false">https://medium.com/p/169834eaa1e8</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[dayjs]]></category>
            <category><![CDATA[momentjs]]></category>
            <dc:creator><![CDATA[Leandro Merli]]></dc:creator>
            <pubDate>Thu, 06 Jan 2022 04:41:29 GMT</pubDate>
            <atom:updated>2022-01-06T04:41:29.131Z</atom:updated>
            <content:encoded><![CDATA[<p>Moment.js has been a great library that helped to deal with Date object and adding Date helper functions (and Timezone) in javascript projects.</p><p>Since 2020 this library was deprecated. Look what the author says:</p><blockquote><em>We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.</em></blockquote><p>What’s the big problem about moment.js? In my opinion it’s the size of it. It’s a really big library.</p><p>So the first question is: How to replace moment and have the less impact and work possible?</p><p>After researching and testing some libraries I decided to use: Day.JS</p><p><a href="https://day.js.org/">https://day.js.org/</a></p><p>The first question is Why do I pick Day.JS?</p><p>First of all, It has almost the same interface than Moment.js. That means a lot if you have a big project</p><p>It has a typescript interface so it’s really cool for Angular Project (or other typescript framework)</p><p>Other good thing is that it is a really small library. After adding all the needed plugins it’s size was 11.84 KB.</p><p>This library has the core functions and then you can add more functions (like plugins)</p><p>The bad thing is that the library doesn’t support treeshaking. But as you can see in the next pic it’s size is really small because the library is broken into multiple plugins so you can add what you really need.</p><p>So you don’t need to worry about treeshaking stuff.</p><p>For us it’s only a small point in the sea. Check it out:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*vz4Zbxce7l301Jln.png" /></figure><p>Look how it’s really small compared to other libraries:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*XeSmVz02eLUI-YuL.png" /></figure><p>Compare it with Moment library:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*t6F6I43111dor7FU.jpg" /></figure><p>We have checked these great articles about the most popular alternatives of Moment.js and these help us to decide</p><p><a href="https://medium.com/swlh/best-moment-js-alternatives-5dfa6861a1eb">https://medium.com/swlh/best-moment-js-alternatives-5dfa6861a1eb</a></p><p>Check this comparison</p><p><a href="https://github.com/you-dont-need/You-Dont-Need-Momentjs#is-before">https://github.com/you-dont-need/You-Dont-Need-Momentjs#is-before</a></p><p>More alternatives</p><p><a href="https://blog.logrocket.com/more-alternatives-to-moment-js/">https://blog.logrocket.com/more-alternatives-to-moment-js/</a></p><p>Comparing trends</p><p><a href="https://www.npmtrends.com/date-fns-vs-dayjs-vs-luxon-vs-moment">https://www.npmtrends.com/date-fns-vs-dayjs-vs-luxon-vs-moment</a></p><h3>Steps to Replace Moment with Day.JS</h3><pre>export class DateFunctions { }</pre><p>The idea is to encapsulate all date functions and the library import in this new class (or service) and have this library only here</p><p><a href="https://day.js.org/docs/en/installation/typescript">https://day.js.org/docs/en/installation/typescript</a></p><pre>import * as dayjs from &#39;dayjs&#39;</pre><p>like this</p><pre>import * as timezone from &#39;dayjs/plugin/timezone&#39; import * as utc from &#39;dayjs/plugin/utc&#39; dayjs. extend (utc) dayjs. extend (timezone)</pre><pre>static new (date = undefined , format = undefined ) : dayjs.Dayjs { if (date === undefined ) { return dayjs ( ) ; } if (format) { return dayjs (date, format) ; } else { return dayjs (date) ; } }</pre><p>then</p><p>Replace moment() to DateFunctions.new() or moment(date) to DateFunctions.new(date)</p><pre>npm uninstall moment moment-timezone --save</pre><p>Other cool library is date-fns which does support treeshacking but I decided not to use it because the interface and functions are different from Moment. So it was more work to replace one from other</p><p>If you want to try to replace with date-fns check this article</p><p><a href="https://artemdemo.com/blog/20181104-replacing-momentjs/">https://artemdemo.com/blog/20181104-replacing-momentjs/</a></p><p>For another project I have also used Luxon. It seems also a really good library, the interface is almost the same as Moment. But as Day.JS it does not support treesshacking</p><p><a href="https://github.com/moment/luxon/issues/854">https://github.com/moment/luxon/issues/854</a></p><p><em>Originally published at </em><a href="https://leandromerli.com/replace-moment-library-angular/"><em>https://leandromerli.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=169834eaa1e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Node.js — Express — Elastic Beanstalk — Mongodb.js]]></title>
            <link>https://medium.com/@leandromerli/node-js-express-elastic-beanstalk-mongodb-js-b3bf84d5fbfc?source=rss-b28813d75e------2</link>
            <guid isPermaLink="false">https://medium.com/p/b3bf84d5fbfc</guid>
            <category><![CDATA[elastic-beanstalk]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Leandro Merli]]></dc:creator>
            <pubDate>Thu, 06 Jan 2022 04:40:02 GMT</pubDate>
            <atom:updated>2022-01-06T04:40:02.375Z</atom:updated>
            <content:encoded><![CDATA[<h3>Node.js — Express — Elastic Beanstalk — Mongodb.js</h3><p>I have started using Node.js with Express framework in a company that wants to move Oracle’s PLSQL procedures that render html and have the process done in Node.js, so ORACLE will be using for only storing data.</p><p>After that good experience I migrated a website built in Wordpress to NodeJs so I can have my own code because I want to do more cool stuff and improve the user experience and the quality of the website.</p><h3>Node JS + Express</h3><p>Express is the framework to build a web server or API easily. It’s really easy to start and have,quickly, our first “hello world”.</p><p>The only thing that can be a problem is that you need to handle the errors and exceptions because those can bring the Express server down. You must handle errors properly and if an error not catched makes the server go down, the code has to be prepared to start a new web server automatically. To accomplish that, you have to build the server inside a cluster.</p><p>The use of cluster also makes it possible that the server can use all CPU core that are available in your infrastructure. Check this article for more information.( <a href="https://expressjs.com/en/advanced/best-practice-performance.html#handle-exceptions-properly">https://expressjs.com/en/advanced/best-practice-performance.html#handle-exceptions-properly</a>)</p><h3>MongoDB</h3><p>I’m very happy with Mongodb. It’s simple to use and the documentation is complete. The biggest benefits for me are that you can save and retrieve JSON objects, I can easily integrate Geo Location based queries (a cool feature) and I can easily change the DB structure.</p><p>What? Yes, I know MongoDB is a structureless database but I have used Mongoose (in my API Node.JS), so the DB structure is there. That’s great because you define all your models in only one place: the API. Those models can be used by Mongoose and also to be used in other parts of your API NodeJS’s code.</p><p>In addition I have a “composite” model definition: I reuse some inner model definitions in several parent models. For example a “Place” model shares the GEO position model and that inner model is also a child model of “Business” model.</p><p>About robustness: I have been running a website for 4 years and that DB never went down or had a slow response in these years.</p><h3>AWS Elastic Beanstalk</h3><p>This is a server technology. It’s part of one of Amazon Web Services. It allows you to have multiple environments and deploy your code from the command line. One cool feature is that when you deploy it, it launches a new server instance so your site never goes offline. If it fails nothing will happen. You still have your “old” instance running.</p><p>The main benefit of using AWS Elastic Beanstalk is that it can launch multiple instances automatically if your server is under high pressure (a lot of traffic or long processing going on). It can be configured to start new instances based on memory usage or cpu usage. You have a settings page where you can configure that load balancer.</p><p>I found it a little difficult to know which threshold to set and what value. So at the end it was a matter of setting a parameter that I felt good with and then see how the server responded.</p><h3>Conclusion</h3><p>I think this stack is great for a lot of applications, it’s robust, safe, quick and easy to manage.</p><p>I really like to develop using the same language in frontend and backend. MongoDB is also great when you don’t need complex queries (like a lot of joints).</p><p>About Elastic Beanstalk the only key point is to not trust in it’s internal memory (RAM) (because your instance can be ended when you deploy or you can even have multiple instances running, when you have set up a load balancer with autoscaling).</p><p>I have integrated it with a payment gateway and it worked really ok.</p><p><em>Originally published at </em><a href="https://leandromerli.com/nodejs-express-elastic-beanstalk-mongodb/"><em>https://leandromerli.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b3bf84d5fbfc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Serverless Image resize]]></title>
            <link>https://awstip.com/serverless-image-resize-3a3633312412?source=rss-b28813d75e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a3633312412</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[serverless-architecture]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[serverless-framework]]></category>
            <dc:creator><![CDATA[Leandro Merli]]></dc:creator>
            <pubDate>Thu, 06 Jan 2022 04:38:21 GMT</pubDate>
            <atom:updated>2022-01-10T10:32:49.737Z</atom:updated>
            <content:encoded><![CDATA[<p>With this technique you will be able to resize images on the fly, in this case with a serverless function. This is a really nice example of what you can do with serverless and very useful if you have a website that has a lot of images.</p><p>Sometimes you redesign the website or you use the same images in a lot of places, within the post or article, in the blog list and in the home page, and all use different sizes.</p><p>The idea is to store images in the server at full size, then you call an endpoint with parameters and you get the image with a different dimension or, even, with a different format (png/jpeg/jpg).</p><p>For example like this</p><pre><a href="https://{server_url}/%7Bimage_id%7D/300x200/jpg">https://{server_url}/{image_id}/300x200/jpg</a></pre><p>This will return a jpg image of 300 width and 200 height</p><p>You have several options to do this, this article will show you how to implement this with AWS lambda. You can build this solution using a proposal from AWS and using a serverless.com proposal (I have used this)</p><h3>AWS approach</h3><p>It’s a Cloudflare template from AWS. It’s uses internally S3, Cloudfront, a Node.js code (which uses Sharp library)</p><p><a href="https://docs.aws.amazon.com/solutions/latest/serverless-image-handler/welcome.html">https://docs.aws.amazon.com/solutions/latest/serverless-image-handler/welcome.html</a></p><p>Internally it’s build with AWS CDK</p><p><a href="https://aws.amazon.com/en/cdk/">https://aws.amazon.com/en/cdk/</a></p><p>You can grab that code.. and modify if necessary.</p><h3>Serverless.com aproach</h3><p>This alternative is using the Serverless Framework (serverless.com). For those who don’t know Serverless Framework. It’s a very simple way to describe the resources you need in your cloud server (I use AWS but works with others) and deploy the code easily from the command line.</p><p>This nice article <a href="https://www.serverless.com/blog/dynamic-image-resizing-nodejs">https://www.serverless.com/blog/dynamic-image-resizing-nodejs</a> describes how to implement the Image Resize on the cloud. This uses AWS S3 and runs with Node.JS code.</p><p>From here you can grab the code base.</p><p><a href="https://github.com/serverless/examples/tree/master/aws-node-dynamic-image-resizer">https://github.com/serverless/examples/tree/master/aws-node-dynamic-image-resizer</a></p><p>You need to be careful when configuring the Amazon S3 bucket. The docs don’t explain with detail these steps I will cover next.</p><h3>How to configure the bucket:</h3><p>1- The bucket has to be public accessible (set as a hosting)</p><p>2- You have to set the bucket policy properly</p><p>3- Set the Amazon S3 bucket as static</p><p>4- Set the redirect rule</p><p>5- Use the correct url</p><h4>Let’s see the steps one by one</h4><p>1- Make sure the bucket is public.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*YKqauuKHF_kJrJL7.png" /></figure><p>2- Copy the policy this policy into the bucket</p><pre>{ &quot;Version&quot; : &quot;2012-10-17&quot; , &quot;Statement&quot; : [ { &quot;Effect&quot; : &quot;Allow&quot; , &quot;Principal&quot; : { &quot;AWS&quot; : &quot;*&quot; } , &quot;Action&quot; : &quot;s3:GetObject&quot; , &quot;Resource&quot; : &quot;arn:aws:s3:::{bucket-name}/*&quot; } ] }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*TgYZAmmMOnhGXstc.png" /></figure><p>3- Set the Amazon S3 bucket as static</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*qGjUfUP_2cc0RLIp.png" /></figure><p>4- Set the redirect Rule</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*Ja6E0k9ybAl-VRjO.png" /></figure><pre>[ { &quot;Condition&quot; : { &quot;HttpErrorCodeReturnedEquals&quot; : &quot;404&quot; } , &quot;Redirect&quot; : { &quot;HostName&quot; : &quot;your-api-url-without-https&quot; , &quot;HttpRedirectCode&quot; : &quot;307&quot; , &quot;Protocol&quot; : &quot;https&quot; , &quot;ReplaceKeyPrefixWith&quot; : &quot;img/&quot; } } ]</pre><p>5- You have to use the “website” url… not the link to the bucket</p><p>Don’t use:</p><pre><a href="https://{bucket-name}.s3.{region}.amazonaws.com/%7Bfilepath%7D">https://{bucket-name}.s3.{region}.amazonaws.com/{filepath}</a></pre><p>Use:</p><pre><a href="http://{bucket-name}.s3-website.{region}.amazonaws.com/%7Bfilepath%7D">http://{bucket-name}.s3-website.{region}.amazonaws.com/{filepath}</a></pre><p>Like</p><pre><a href="http://{bucket-name}.s3-website.{region}.amazonaws.com/300x200/%7Bfilepath%7D">http://{bucket-name}.s3-website.{region}.amazonaws.com/300x200/{filepath}</a></pre><p>This will get the image with {filepath} redimensioned to 300px width and 200px height</p><p><em>Originally published at </em><a href="https://leandromerli.com/serverless-image-rezise/"><em>https://leandromerli.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a3633312412" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/serverless-image-resize-3a3633312412">Serverless Image resize</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Universal: Do and Don’t]]></title>
            <link>https://medium.com/@leandromerli/angular-universal-do-and-dont-c34ba07ef480?source=rss-b28813d75e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c34ba07ef480</guid>
            <category><![CDATA[server-side-rendering]]></category>
            <category><![CDATA[angular-universal]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Leandro Merli]]></dc:creator>
            <pubDate>Thu, 06 Jan 2022 04:37:22 GMT</pubDate>
            <atom:updated>2022-01-06T04:37:22.502Z</atom:updated>
            <content:encoded><![CDATA[<p>What is Angular Universal?</p><p>Angular Universal can be described as the process of rendering your application to HTML on the Server (ie: Node.js) aka server-side rendering (SSR), as opposed to typical Angular applications such as Single-Page Applications (SPA’s) where the rendering occurs on the Browser, a process that can also be referred to as client-side rendering (CSR).</p><h3>What I have learned:</h3><ul><li>Use Lazy loading: Always!</li></ul><p>Lazy loading is the process of splitting the app in multiple modules. When the user goes to a different route, the browser will download and execute the module. My suggestion is to have one module per route/component, because it will improve your SEO rank, the “home” of your site will be smaller and faster to load. But this is not only important for SEO, if you also have a regular Angular App it is always important to use Lazy Loading. Tip: Lazy loading is not only for routes, you can load modules inside a component (this is useful for heavy components with subcomponents).</p><ul><li>Don’t use shared module libraries</li></ul><p>When you begin a project you will be tempted to make a shared module and have in there all the basic or common components and libraries used across all components. That’s a big mistake when you want your pages/app to load quickly because there will be some pages that don’t need a particular library or component but it’s loaded because you added those in that shared library. So take the time and have all the modules with the specific libraries you need.</p><ul><li>Use Transfer state</li></ul><p>The page is rendered on the server side, so Angular will call all endpoints to fetch data needed. After that the browser will show the UI and Angular is executed again!. It will fetch all data again. With transfer state the rendered page, will have the data injected (internally it’s a json in the html), avoiding calling the endpoints again to grab the data that the page needs.</p><ul><li>Minimize the use of external node libraries</li></ul><p>Use the visual framework you choose (like for example Angular Material) and try to make your own components. Sometimes the libraries are not updated or have issues with the new Angular Ivy compiler.</p><ul><li>Avoid Moment.js</li></ul><p>This library is currently deprecated so avoid using it. <a href="https://momentjs.com/docs/#/-project-status/">https://momentjs.com/docs/#/-project-status/</a> If you are using it, try to remove it using native javascript Date or you can use other libraries like Luxon that has a similar API than moment</p><ul><li>Analyze size of all modules you generate</li></ul><p>You have an easy way to check the size of each module/bundle <a href="https://dev.to/salimchemes/analyzing-angular-bundle-with-webpack-bundle-analyzer-29gp">https://dev.to/salimchemes/analyzing-angular-bundle-with-webpack-bundle-analyzer-29gp</a> After applying the tool you can find any mistake like including components or libraries that you don’t need for an UI or find out libraries that are too heavy.</p><ul><li>Create your own component’s library(s) and split it from the “website” main project</li></ul><p>When you start a new project, you create a the project repository and start adding components to it, and when the project grows or your project’s components need to be use by other projects, you may need to create your own library of components <a href="https://angular.io/guide/creating-libraries">https://angular.io/guide/creating-libraries</a> My recommendation is to start from the beginning of the project with an angular library (or severals), even for small projects. It’s easy to maintain and reuse components and modules. Also better find out errors with the new Ivy compiler.</p><ul><li>Think first the infrastructure</li></ul><p>If you want to have a quick loading website, start thinking first about the whole infrastructure and next you can build a proof of concept to actually test the whole picture.</p><ul><li>Use a CDN to cache</li></ul><p>To speed up a site and avoid using your server for the same render task again and again you have to evaluate using a caching system or CDN.</p><p>You can have your caching system after render a page, saving the content in DynamoDB and then when a new user request that page, grab the content form the DB.</p><p>But the best alternative it’s to use an CDN, with that you can mirror your site in different servers that can be close to your visitor location (that will improve latency).</p><p>The only concern about CDN it’s the price, if you are in AWS you must check Cloudfront prices ( <a href="https://aws.amazon.com/en/cloudfront/pricing/">https://aws.amazon.com/en/cloudfront/pricing/</a>) or you may evaluate using Cloudflare which has a free tier (personally I have use it a lot)</p><p><em>Originally published at </em><a href="https://leandromerli.com/angular-universal-tips/"><em>https://leandromerli.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c34ba07ef480" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Universal + Serverless.com + Aws Lambda + DynamoDB]]></title>
            <link>https://medium.com/@leandromerli/angular-universal-serverless-com-aws-lambda-dynamodb-1a75b006c20b?source=rss-b28813d75e------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a75b006c20b</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[dynamodb]]></category>
            <category><![CDATA[angular-universal]]></category>
            <category><![CDATA[lambda]]></category>
            <dc:creator><![CDATA[Leandro Merli]]></dc:creator>
            <pubDate>Sun, 02 Jan 2022 16:03:13 GMT</pubDate>
            <atom:updated>2022-01-06T04:45:09.753Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I decided to take the serverless approach to build first a “fast reading course”. This course consists of a different set of exercises to improve your reading speed.</p><p>It’s a simple database structure with a lot of info (mainly for the text information). I needed to store the student related info, the exercise texts (like a dictionary of words and several small texts), the admin/teacher info and the course structure.</p><p>I chose DynamoDB as the database (the decision to use DynamoDB and not for example MongoDB was because the cost structure on AWS, it is almost free when developing) I used Angular for the frontend (lot of experience using Angular) and AWS Lambda to run the API functions (let’s test something new!).</p><p>This was the first app I built successfully, then I had to make a CMS to build the website of the course and allow users to buy online. In order to continue using Angular as the frontend and make it SEO friendly, I had to use Angular Universal.</p><p>For the authentication I integrated the app with AWS Cognito so I could reuse the functions already created by Amazon to authenticate and deliver email when registering.</p><p>The first question I had was how to build and deploy the AWS Lambda functions. I tested SAM CLI ( <a href="https://aws.amazon.com/en/serverless/sam/">https://aws.amazon.com/en/serverless/sam/</a>) , I successfully created a hello world endpoint locally but then I tested serverless.com framework and I liked it more (Reasons: I saw it as a more simple approach).</p><p>So the stack was this:</p><ul><li>Authentication: AWS Cognito</li><li>Serverless.com API Deploy</li><li>Aws Lambda</li><li>DynamoDB as the database</li><li>Angular Universal (Frontend and SSR)</li></ul><h3>AWS Cognito</h3><p>For those who don’t know it, it’s like a series of endpoints and functions, for the auth process, that you can use again so you don’t waste time on it.</p><p>Instead of calling an API endpoint you call a cognito endpoint from your UI or inside your API.</p><p>The first question was if I needed to have my own users table or I can store all info of the users in cognito. The answer is NO you have to have the users table and store the cognito id.</p><p>That Cognito Id internally is the userSub returned by the create user endpoint. You must store that userSub as your id for the created user.</p><p>The other question was: What compatible Angular Library to use to connect to cognito?. After some time “googling” I found Amazon Cognito Identity JS ( <a href="https://www.npmjs.com/package/amazon-cognito-identity-js">https://www.npmjs.com/package/amazon-cognito-identity-js</a> ), not sure why it was not easy to find. Here I found the docs <a href="https://datacadamia.com/aws/cognito/js_identity">https://datacadamia.com/aws/cognito/js_identity</a></p><p>Amazon is trying you to use AWS Amplify but I was not sure whether to use it or not. I had some code already done so I decided not to use it (Also refer to this <a href="https://peter-sereda.medium.com/why-you-may-want-not-use-aws-amplify-for-your-next-serverless-project-57b578e60252">https://peter-sereda.medium.com/why-you-may-want-not-use-aws-amplify-for-your-next-serverless-project-57b578e60252</a>)</p><p>Other consideration is that if you want to provide a confirmation URL in your Create User API endpoint the email template must be added in the API (also for “forgot my password). That’s because in the Amazon Cognito configuration you have a limitation in the amount of characters you can enter for the email template and also it’s not dynamic.</p><h3>Serverless.com</h3><p>It’s a framework to easily deploy to AWS and it configures all the resources you need there. It uses YML language to declare all the services.</p><p>It has some plugins that you can use:</p><ul><li>serverless-bundle: it uses webpack to build your local js</li><li>serverless-domain-manager: custom domain for your endpoints url</li><li>serverless-offline: in order to test locally</li><li>serverless-dynamodb-local: it open a local dynamo database</li></ul><p>The setup was easy, so I decided to use it.</p><p>The only drawback was that after I built several endpoints, I deployed to my dev environment (in AWS, not locally) and I got the error of “More than 200 hundred resources” see here: <a href="https://www.serverless.com/blog/serverless-workaround-cloudformation-200-resource-limit/">https://www.serverless.com/blog/serverless-workaround-cloudformation-200-resource-limit/</a> The solution was to split into several microservices (differents YML files)</p><p>I recommend to split 1 service per DB table or by logical or so you have a maximum of 10 endpoints (ideally 5) endpoints per service file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/0*bhQUHLWrpKDSXawy.png" /></figure><p>The serverless-base-aws has all the table and S3 bucket definitions.</p><p>After all, I’m happy with Serverless framework, it’s easy to deploy. However if I start a new project I would check first AWS CDK <a href="https://aws.amazon.com/en/cdk/">https://aws.amazon.com/en/cdk/</a> with the intention to see what’s the current best option, but as I had said I’m happy with serverless.</p><h3>AWS Lambda</h3><p>Serverless it’s the “deploy” framework to AWS but behind the scenes it creates several Lambda functions. Those are the real serverless functions that you can trigger calling an url that AWS provides. For the majority of projects you would have to set up a custom domain.</p><p>I’m really a fan of all these serverless approaches to build an API. You have some pros and cons but for my structure it worked really well.</p><p>The only consideration for me was to increase the memory size even if my API functions do simple things (like just updating a Dynamodb record), the reason is that more memory means more CPU speed and that increases the response time of the server.</p><p>You can check a nice article about CPU speed, memory usage and cost on AWS Lambda <a href="https://medium.com/geekculture/pick-the-right-memory-size-for-your-aws-lambda-functions-682394aa4b21">https://medium.com/geekculture/pick-the-right-memory-size-for-your-aws-lambda-functions-682394aa4b21</a></p><h3>DynamoDB</h3><p>DynamoDB it’s a document (json) database, so it’s easy to use in a Node.js project.</p><p>The good thing about Dynamodb is that you can have dev, stage and production database for free or at a really small fee (a really good thing for startup companies) , also it’s easy to use when you are in the AWS ecosystem.</p><p>I have used MongoDB before and, if I need to choose again, I think the only reason I will choose DynamoDB it’s because it’s free when you are developing. I think MongoDB has more features and it’s easier to query, especially geo queries.</p><h3>Angular + Angular Universal</h3><p>I have used Angular for production projects for 4 years now. I have no complaints. I think it’s a great framework, robust and you can build real complex apps.</p><p>I had to integrate Angular Universal into the project because it had to add visibility for search engines. I had zero experience using it so I grabbed an Angular Universal project from github and added the files into my project. After some time “fighting” with it I had my site running SEO friendly.</p><p>It’s really important to check where you are using Browser only functions or objects, like Local Storage or the window object. You have to replace those with your own implementation. Check this out for Local Storage: <a href="https://santibarbat.medium.com/how-to-use-localstorage-with-angular-9-universal-ssr-517578615637">https://santibarbat.medium.com/how-to-use-localstorage-with-angular-9-universal-ssr-517578615637</a></p><p>Also it is really important to add Transfer State, which is a way to inject the data you fetch from the server and add that to the rendered app. Then, in the browser, the app will load the data from there and not call the API again. The problem is that Angular renders twice. Once on the server side, and then the second time after the browser loads the javascript code. You must stop that in order to really improve the load speed and also avoid extra charges for executing twice each endpoint.</p><p>Other consideration if you want to have a real SEO friendly site you also have to change the title per page, add to the HTML head the description and other metatags that the Website Crawlers read.</p><h3>Conclusion</h3><p>Building a serverless app and using all AWS services has the benefit that in development time you don’t need to pay for the resources (or almost nothing).</p><p>I literally pay 0.50 per domain in Route S3. For garage companies or experimental projects it’s really a good start.</p><p>Also you have the other benefits: A scalable structure when the project grows, amazing performance (it depends on what you can pay), a variety of server resources to complete your tasks and decide what is best for the project.</p><p>For the frontend Angular is a good framework to use, complete, you don’t need to find any other library or tool.</p><p>If you are not using Angular Universal you can deploy easily your Angular UI to Amazon S3 bucket with a serverless plugin called serverless-finch ( <a href="https://www.npmjs.com/package/serverless-finch">https://www.npmjs.com/package/serverless-finch</a>). Then access the bucket by a custom domain or subdomain</p><p><em>Originally published at </em><a href="https://leandromerli.com/angular-universal-serverless-lambda-dynamodb/"><em>https://leandromerli.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a75b006c20b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>