<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Akash Rajpurohit</title>
    <description>The latest articles on Forem by Akash Rajpurohit (@akashrajpurohit).</description>
    <link>https://forem.com/akashrajpurohit</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F393909%2F0ab73809-a4d3-43f7-bf63-682c76ef3d26.jpg</url>
      <title>Forem: Akash Rajpurohit</title>
      <link>https://forem.com/akashrajpurohit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/akashrajpurohit"/>
    <language>en</language>
    <item>
      <title>Getting paid in foreign currency as a software developer in India</title>
      <dc:creator>Akash Rajpurohit</dc:creator>
      <pubDate>Fri, 21 Feb 2025 11:24:01 +0000</pubDate>
      <link>https://forem.com/akashrajpurohit/getting-paid-in-foreign-currency-as-a-software-developer-in-india-13j7</link>
      <guid>https://forem.com/akashrajpurohit/getting-paid-in-foreign-currency-as-a-software-developer-in-india-13j7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As the world becomes increasingly remote-first, more Indian software developers are finding opportunities to work with clients and companies based outside India. While this opens up exciting possibilities for career growth and better compensation, it also comes with its own set of challenges - particularly around receiving payments and staying compliant with Indian regulations.&lt;/p&gt;

&lt;p&gt;After spending considerable time researching this topic, going through countless blogs, Reddit threads, and discussions with fellow developers and professionals, I realized there wasn't a comprehensive guide that addressed all aspects of receiving foreign income as an Indian developer. This blog post aims to fill that gap.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article is based on my personal research and experience. While I strive to provide accurate information, I am not a Chartered Accountant (CA) or tax expert.&lt;/p&gt;

&lt;p&gt;Tax laws and regulations can change, and individual situations may vary. Please consult with a qualified CA or tax professional for advice specific to your situation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Receiving Payments from Foreign Clients
&lt;/h2&gt;

&lt;p&gt;One of the first challenges you'll face is deciding how to receive payments from foreign clients. The reason why this is a challenge and tricky thing to do is because you are dealing with foreign currency and at the end of the day, you need to convert that foreign currency to Indian Rupees.&lt;/p&gt;

&lt;p&gt;If you are doing this less frequently then maybe you don't need to worry much about this, but if you are going to be receiving payments from foreign clients on a regular basis then you need to find a way to optimize this process as much as possible.&lt;/p&gt;

&lt;p&gt;The reason is simple, a small difference in currency exchange rates, transaction fees, bank charges and processing fees and time can add up to a significant amount of money over time (as much as &lt;strong&gt;4-5% of the total amount&lt;/strong&gt; which can be a lot depending on your volume of transactions).&lt;/p&gt;

&lt;p&gt;With that being said, let's look at the options available to you.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Direct Wire Transfer (with SWIFT code)
&lt;/h3&gt;

&lt;p&gt;The traditional method of receiving foreign payments directly to your bank account. How this process works is that you will open a current account in your name which has global trade account setup.&lt;/p&gt;

&lt;p&gt;Once that is done, you will be provided with the account number and the SWIFT code of the bank. You can then use this information to receive payments from your foreign clients.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No intermediary fees&lt;/li&gt;
&lt;li&gt;Direct deposit to your bank&lt;/li&gt;
&lt;li&gt;Transparent process&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High transfer fees (both sender and receiver)&lt;/li&gt;
&lt;li&gt;Poor exchange rates&lt;/li&gt;
&lt;li&gt;Longer processing time&lt;/li&gt;
&lt;li&gt;Complex documentation requirements&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;To find what exchange rate you will be getting from different banks, search for "&lt;strong&gt;TT buying rate bank-name&lt;/strong&gt;" on Google. Each bank releases their daily exchange rates on their website. Check the rates for your foreign currency and see how much less than the market rate you will be getting.&lt;/p&gt;

&lt;p&gt;Now, coming back to the reality, what you can do instead is you can go to the bank, talk to the manager (or any representative who handles the foreign income transactions) and ask them for a better rate. Based on the volume of the transaction you plan to do, they might give you a better rate.&lt;/p&gt;

&lt;p&gt;So if you are planning to do it via this way, then I would highly recommend that you talk and get the best rate possible. You can even get &lt;strong&gt;15-20 paise&lt;/strong&gt; less than the current market rate if you are doing a large volume of transactions. To compare this with standard market rate, you might be getting &lt;strong&gt;1-1.5 rupees&lt;/strong&gt; less than the market rate which is awfully low by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Skydo
&lt;/h3&gt;

&lt;p&gt;A relatively new player in the market, &lt;a href="https://go.akashrajpurohit.com/a/skydo" rel="noopener noreferrer"&gt;Skydo&lt;/a&gt; is designed for Indian freelancers and agencies receiving foreign payments.&lt;/p&gt;

&lt;p&gt;Few things that I found quite interesting about them is firstly they give you the live FX rates (which means that what you see is what you get) and their pricing model is very simple with a flat fee based on the amount of money you are receiving which makes it very transparent and easy to understand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Competitive exchange rates&lt;/li&gt;
&lt;li&gt;Lower fees compared to traditional banks&lt;/li&gt;
&lt;li&gt;Designed specifically for Indian businesses&lt;/li&gt;
&lt;li&gt;Quick setup and easy documentation&lt;/li&gt;
&lt;li&gt;Direct integration with accounting software&lt;/li&gt;
&lt;li&gt;Excellent customer support (I can vouch for this personally)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limited to specific countries&lt;/li&gt;
&lt;li&gt;Relatively new service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The flow will look like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You will be provided with a virtual bank account number depending on the currency and country you are receiving the payment from.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if you are receiving the payment from the USA, you will be provided with a USA bank account number, ACH / Fedwire details etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transfer the money to your Skydo account using those details.&lt;/li&gt;
&lt;li&gt;Link your Indian bank account to your Skydo account by providing the account number and IFSC code.&lt;/li&gt;
&lt;li&gt;Once you receive the payment, you will be able to see the amount in your Skydo dashboard which will get converted and transferred to your Indian bank account.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Wise (Formerly TransferWise)
&lt;/h3&gt;

&lt;p&gt;A popular modern solution for international transfers. This was probably the first one that comes into mind when you think about receiving payments from foreign clients.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better exchange rates than banks&lt;/li&gt;
&lt;li&gt;Lower fees&lt;/li&gt;
&lt;li&gt;Transparent pricing&lt;/li&gt;
&lt;li&gt;Multiple currency support&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;May require additional documentation&lt;/li&gt;
&lt;li&gt;Some banks may have issues with Wise transfers&lt;/li&gt;
&lt;li&gt;Not all countries supported&lt;/li&gt;
&lt;li&gt;Recently their sign up process has become quite of a hassle for indian users and hence I have not been able to test it out myself.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. PayPal
&lt;/h3&gt;

&lt;p&gt;While popular globally, PayPal might not be the best option for Indian recipients for larger amounts but it is still a good option to consider for smaller and infrequent transactions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Widely accepted&lt;/li&gt;
&lt;li&gt;Easy to set up&lt;/li&gt;
&lt;li&gt;Good for small amounts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High fees (from my personal experience its the worst, even compared to the direct wire transfer)&lt;/li&gt;
&lt;li&gt;Poor exchange rates&lt;/li&gt;
&lt;li&gt;Funds can be held without notice&lt;/li&gt;
&lt;li&gt;Withdrawal to Indian banks can be slow&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Understanding Tax Implications
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Section 44ADA: A Blessing for Professionals
&lt;/h3&gt;

&lt;p&gt;If your gross receipts are under ₹75 lakhs per year, Section 44ADA of the Income Tax Act can be particularly beneficial.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key points about 44ADA:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Presumptive Taxation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;50% of your gross receipts are considered as profit&lt;/li&gt;
&lt;li&gt;Remaining 50% is assumed as business expenses&lt;/li&gt;
&lt;li&gt;No need to maintain detailed books of accounts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Eligibility:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Applicable to professionals (including software developers)&lt;/li&gt;
&lt;li&gt;Gross receipts should not exceed ₹75 lakhs in a financial year (provided 95% of the income is via online mode and not cash)&lt;/li&gt;
&lt;li&gt;Only available to individuals and partnership firms (not companies)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Benefits:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simplified tax filing&lt;/li&gt;
&lt;li&gt;No need for audit&lt;/li&gt;
&lt;li&gt;Can still claim actual expenses if they're lower than 50%&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Beyond ₹75 Lakhs: Regular Taxation
&lt;/h3&gt;

&lt;p&gt;If your income exceeds ₹75 lakhs, you'll need to:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Maintain Books of Accounts:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Regular books of accounts required&lt;/li&gt;
&lt;li&gt;Need professional help (CA) for maintenance&lt;/li&gt;
&lt;li&gt;Annual audit mandatory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Actual Expense Deduction:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can claim actual business expenses&lt;/li&gt;
&lt;li&gt;Need to maintain proper documentation&lt;/li&gt;
&lt;li&gt;Common deductible expenses include:

&lt;ul&gt;
&lt;li&gt;Home office expenses&lt;/li&gt;
&lt;li&gt;Internet and phone bills&lt;/li&gt;
&lt;li&gt;Computer equipment and software&lt;/li&gt;
&lt;li&gt;Professional development courses&lt;/li&gt;
&lt;li&gt;Travel expenses (if applicable)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;p&gt;One important thing to note here is that you need to make sure whatever platform you use, it is compliant with the Indian regulations and laws and also that you are receiving FIRA/FIRC document from the bank for each of your transactions.&lt;/p&gt;

&lt;p&gt;This document is required by the Indian government to prove that the money you are receiving is from a foreign source and not from an Indian source and will be very much needed when you file your taxes and want to claim the foreign income under 44ADA.&lt;/p&gt;

&lt;p&gt;PS: With &lt;a href="https://go.akashrajpurohit.com/a/skydo" rel="noopener noreferrer"&gt;Skydo&lt;/a&gt;, you don't need to worry about this as they will provide you with the FIRA document for each of your transactions.&lt;/p&gt;

&lt;h2&gt;
  
  
  GST Registration and Compliance
&lt;/h2&gt;

&lt;h3&gt;
  
  
  When to Register for GST
&lt;/h3&gt;

&lt;p&gt;GST registration is mandatory if you're providing services to foreign clients (considered as export of services) and you are earning more than 20 lakhs in a financial year.&lt;/p&gt;

&lt;p&gt;While you will be registering for GST, you need not worry about charging your foreign clients for the GST as it will be zero rated for you. To enable this you will be applying for the LUT (Letter of Undertaking) from the GST portal for every financial year.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key points about GST:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Registration Requirements:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mandatory regardless of turnover for export services&lt;/li&gt;
&lt;li&gt;Need to register in the state where you're based&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Documentation Needed:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PAN card&lt;/li&gt;
&lt;li&gt;Aadhaar card&lt;/li&gt;
&lt;li&gt;Bank account details&lt;/li&gt;
&lt;li&gt;Address proof&lt;/li&gt;
&lt;li&gt;Passport size photograph&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  GST Compliance Requirements
&lt;/h3&gt;

&lt;p&gt;Once registered, you need to:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;File Regular Returns:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GSTR-1 (monthly/quarterly)&lt;/li&gt;
&lt;li&gt;GSTR-3B (monthly/quarterly)&lt;/li&gt;
&lt;li&gt;Annual returns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Maintain Records:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invoices&lt;/li&gt;
&lt;li&gt;Payment receipts&lt;/li&gt;
&lt;li&gt;Bank statements&lt;/li&gt;
&lt;li&gt;Other relevant documents&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Zero-rated Supply:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Export of services is considered zero-rated&lt;/li&gt;
&lt;li&gt;No GST charged to foreign clients&lt;/li&gt;
&lt;li&gt;Can claim input tax credit&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Consider using accounting software that supports GST compliance and can generate GST-compliant invoices. This will save you significant time and reduce errors in compliance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Best Practices and Tips
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Separate Bank Account:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maintain a separate bank account for business transactions&lt;/li&gt;
&lt;li&gt;Makes accounting and tax filing easier&lt;/li&gt;
&lt;li&gt;Better tracking of income and expenses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Documentation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep all client agreements in writing&lt;/li&gt;
&lt;li&gt;Maintain proper invoices&lt;/li&gt;
&lt;li&gt;Document all business expenses&lt;/li&gt;
&lt;li&gt;Keep foreign inward remittance certificates (FIRC)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Professional Help:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Engage a CA for tax planning and compliance&lt;/li&gt;
&lt;li&gt;Consider using professional accounting software&lt;/li&gt;
&lt;li&gt;Stay updated with regulatory changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Currency Management:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consider maintaining a multi-currency account&lt;/li&gt;
&lt;li&gt;Plan for currency fluctuations&lt;/li&gt;
&lt;li&gt;Use forward contracts if dealing with large amounts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Regular Compliance:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File returns on time&lt;/li&gt;
&lt;li&gt;Pay advance tax if applicable&lt;/li&gt;
&lt;li&gt;Keep track of important dates and deadlines&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Tools and Resources
&lt;/h2&gt;

&lt;p&gt;Here are some tools and resources that can help you manage your finances better:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Payment Platforms:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://go.akashrajpurohit.com/a/skydo" rel="noopener noreferrer"&gt;Skydo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://wise.com" rel="noopener noreferrer"&gt;Wise&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.paypal.com/business" rel="noopener noreferrer"&gt;PayPal Business&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tax Resources:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.incometax.gov.in/" rel="noopener noreferrer"&gt;Income Tax Portal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.gst.gov.in/" rel="noopener noreferrer"&gt;GST Portal&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Working with foreign clients can be rewarding both professionally and financially. While the initial setup and compliance requirements might seem overwhelming, with proper planning and professional help, you can create a smooth system for managing your international income.&lt;/p&gt;

&lt;p&gt;Remember to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose the right payment platform&lt;/li&gt;
&lt;li&gt;Understand your tax obligations&lt;/li&gt;
&lt;li&gt;Stay compliant with GST requirements&lt;/li&gt;
&lt;li&gt;Maintain proper documentation&lt;/li&gt;
&lt;li&gt;Seek professional help when needed&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Have you been working with foreign clients? What challenges have you faced? Share your experiences in the comments below, or reach out to me on &lt;a href="https://twitter.com/akashwhocodes" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; / &lt;a href="https://www.reddit.com/user/Developer_Akash/" rel="noopener noreferrer"&gt;Reddit&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy freelancing! 🚀&lt;/p&gt;

</description>
      <category>finance</category>
      <category>developer</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Increase Swap Memory on Raspberry Pi</title>
      <dc:creator>Akash Rajpurohit</dc:creator>
      <pubDate>Sat, 24 Feb 2024 07:01:30 +0000</pubDate>
      <link>https://forem.com/akashrajpurohit/increase-swap-memory-on-raspberry-pi-m6</link>
      <guid>https://forem.com/akashrajpurohit/increase-swap-memory-on-raspberry-pi-m6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;A Swap file is a file on the disk that can be used to extend the RAM on your system. It is used when the RAM is full and your system needs more memory resources. It is also used to store the data that is not accessed frequently.&lt;/p&gt;

&lt;p&gt;This is especially useful on Raspberry Pi with limited amount of RAM. For example, I have a Raspberry Pi Zero 2W which has 512MB of RAM. When I started adding more services on it, I could see with &lt;a href="https://akashrajpurohit.com/blog/mastering-process-monitoring-and-management-with-htop-command-in-linux/?ref=dev.to-blog"&gt;&lt;code&gt;htop&lt;/code&gt;&lt;/a&gt; that the RAM is becoming almost full. This is where the swap file comes in handy.&lt;/p&gt;

&lt;p&gt;The default swap file size on Raspberry Pi is 100MB. This is might be enough for some use cases, but if you need more, you can increase it.&lt;/p&gt;

&lt;p&gt;Let's see how to do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Increase Swap Memory
&lt;/h2&gt;

&lt;p&gt;We will use the &lt;code&gt;dphys-swapfile&lt;/code&gt; package to increase the swap memory. This package is installed by default on Raspberry Pi OS. If you don't have it installed, you can install it with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;dphys-swapfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First thing to do it to turn off the swap file to avoid any issues:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;dphys-swapfile swapoff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we will update the &lt;code&gt;CONF_SWAPSIZE&lt;/code&gt; variable in the &lt;code&gt;/etc/dphys-swapfile&lt;/code&gt; file. This variable is used to set the size of the swap file. The default value is 100MB. We will set it to 500MB&lt;/p&gt;

&lt;p&gt;I am using &lt;code&gt;vi&lt;/code&gt; to edit the file, but you can use any editor you like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;vi /etc/dphys-swapfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Search for the &lt;code&gt;CONF_SWAPSIZE&lt;/code&gt; variable and update it to &lt;code&gt;500&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CONF_SWAPSIZE=500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the file and exit the editor.&lt;/p&gt;

&lt;p&gt;Now we will reinitialize the swap file setup using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;dphys-swapfile setup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we will turn on the swap file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;dphys-swapfile swapon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once this is done, we can reboot the system to make sure everything is working fine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;reboot
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the reboot, you can check the swap file size using the &lt;code&gt;htop&lt;/code&gt; command and check for the &lt;code&gt;Swp&lt;/code&gt; value, it should be &lt;code&gt;500M&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One thing to note is if you want to increase the swap file size to more than &lt;code&gt;2GB&lt;/code&gt; then you would need to uncomment the &lt;code&gt;CONF_MAXSWAP&lt;/code&gt; variable in the &lt;code&gt;/etc/dphys-swapfile&lt;/code&gt; file and set it to the desired value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I have personally never needed it, but if you do, then you can do it (not verified by me).&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post, we saw how to increase the swap memory on Raspberry Pi. This can be useful if you are running out of RAM on your Raspberry Pi.&lt;/p&gt;

&lt;p&gt;I hope you liked this post. If you have any questions or feedback, reach out to me on &lt;a href="https://twitter.com/AkashWhoCodes"&gt;X / Twitter&lt;/a&gt; or follow my blog at &lt;a href="https://akashrajpurohit.com/?ref=dev.to-blog"&gt;akashrajpurohit.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See you in the next post. Until then, keep HomeLabbing! 👋&lt;/p&gt;

</description>
      <category>raspberrypi</category>
      <category>homelab</category>
      <category>linux</category>
    </item>
    <item>
      <title>Fix Sudo not asking Password on Raspberry Pi</title>
      <dc:creator>Akash Rajpurohit</dc:creator>
      <pubDate>Sat, 24 Feb 2024 06:54:04 +0000</pubDate>
      <link>https://forem.com/akashrajpurohit/fix-sudo-not-asking-password-on-raspberry-pi-kmi</link>
      <guid>https://forem.com/akashrajpurohit/fix-sudo-not-asking-password-on-raspberry-pi-kmi</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When you install a fresh copy of Debian or Raspbian on your Raspberry Pi you might notice that sudo does not ask for a password. This is a security risk and should be fixed. Let's see how to fix it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix Sudo not asking Password on Raspberry Pi
&lt;/h2&gt;

&lt;p&gt;This happens due to the &lt;code&gt;NOPASSWD&lt;/code&gt; option for your user. We need to make sure that this option is not set.&lt;/p&gt;

&lt;p&gt;We will edit the file named &lt;code&gt;010_pi-nopasswd&lt;/code&gt;. To find its location we will use the &lt;a href="https://dev.to/blog/mastering-the-art-of-file-and-directory-search-with-linuxs-find-command/"&gt;&lt;code&gt;find&lt;/code&gt;&lt;/a&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;find / &lt;span class="nt"&gt;-name&lt;/span&gt; 010_pi-nopasswd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will return the location of the file. In my case it is &lt;code&gt;/etc/sudoers.d/010_pi-nopasswd&lt;/code&gt;. Now we will edit this file using &lt;code&gt;visudo&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;visudo /etc/sudoers.d/010_pi-nopasswd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The current content on the file will look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;username&amp;gt; &lt;span class="nv"&gt;ALL&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;ALL&lt;span class="o"&gt;)&lt;/span&gt; NOPASSWD: ALL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;code&gt;&amp;lt;username&amp;gt;&lt;/code&gt; is your username. We need to remove the &lt;code&gt;NOPASSWD&lt;/code&gt; option. So the file will look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;username&amp;gt; &lt;span class="nv"&gt;ALL&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;ALL&lt;span class="o"&gt;)&lt;/span&gt; ALL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the file and exit. Now when you run &lt;code&gt;sudo&lt;/code&gt; it will ask for a password.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We looked into how to fix sudo not asking password on Raspberry Pi. This is a security risk and should be fixed.&lt;/p&gt;

&lt;p&gt;Follow my blog at &lt;a href="https://akashrajpurohit.com/?ref=dev.to-blog"&gt;akashrajpurohit.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>raspberrypi</category>
      <category>homelab</category>
    </item>
    <item>
      <title>How to use ES6 import syntax in Node.js</title>
      <dc:creator>Akash Rajpurohit</dc:creator>
      <pubDate>Fri, 09 Jul 2021 13:52:39 +0000</pubDate>
      <link>https://forem.com/akashrajpurohit/how-to-use-es6-import-syntax-in-node-js-2oak</link>
      <guid>https://forem.com/akashrajpurohit/how-to-use-es6-import-syntax-in-node-js-2oak</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;A module is a JavaScript files that can export one or more values. These values can be variables, objects, or even functions.&lt;/p&gt;

&lt;p&gt;JavaScript does not have built-in support for modules, but the community has created impressive work-arounds. The two most important standards are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CommonJs Modules&lt;/strong&gt; - What we know as the &lt;code&gt;require()&lt;/code&gt; method for importing modules and &lt;code&gt;module.exports&lt;/code&gt; for exporting. This is designed for synchronous loading and mainly used for server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Asynchronous Module Definition (AMD)&lt;/strong&gt; - The most popular implementation of this standard is &lt;a href="https://requirejs.org/"&gt;RequireJS&lt;/a&gt;. This is designed for asynchronous loading and mainly used for browsers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ES6 Import
&lt;/h2&gt;

&lt;p&gt;The goal for ECMAScript 6 modules was to create a format that both users of CommonJS and of AMD are happy with. The syntax for importing modules looks like this..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// for default imports&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// for named imports&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and exporting with named and default exports like this..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MAX_USERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Named exports&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hi &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Default export&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to use it
&lt;/h2&gt;

&lt;p&gt;If you try to run this code directly then you would get an error something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;SyntaxError: Cannot use import statement outside a module
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you are using Node.js version 14.x.x or above then the simplest fix is to add "type": "module" in your package.json file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"module"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However if you are on any version below 14, you would have to use Babel to compile your code before running it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: Babel is not framework or platform opinionated. This means that it can be used in any project that is written in JavaScript and thus, in a Node.js project as well.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's start by installing some dev dependencies,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add &lt;span class="nt"&gt;-D&lt;/span&gt; @babel/core @babel/preset-env @babel/node

&lt;span class="c"&gt;# OR&lt;/span&gt;

npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-D&lt;/span&gt; @babel/core @babel/preset-env @babel/node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next is to create a &lt;code&gt;babel.config.json&lt;/code&gt; file and add the following config,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"presets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"@babel/preset-env"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you just have to use &lt;code&gt;babel-node&lt;/code&gt; instead of node, so your start/dev script may look like this now&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"nodemon --exec babel-node server.js"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this was helpful. See you in another one 👋&lt;/p&gt;

</description>
      <category>node</category>
      <category>es6</category>
      <category>import</category>
      <category>module</category>
    </item>
    <item>
      <title>Increase Root Partition Size On Fedora</title>
      <dc:creator>Akash Rajpurohit</dc:creator>
      <pubDate>Mon, 01 Mar 2021 14:38:05 +0000</pubDate>
      <link>https://forem.com/akashrajpurohit/increase-root-partition-size-on-fedora-48e1</link>
      <guid>https://forem.com/akashrajpurohit/increase-root-partition-size-on-fedora-48e1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I recently was in the need of extending the root partition size on my system and spent quite some time figuring out the approach to follow in doing so without losing data.&lt;/p&gt;

&lt;p&gt;My scenario was I wanted to shrink the /home partition and use that space to increase root partition size. In this blog, I will be sharing how I was able to do the same quickly as possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backup, Backup, and Backup
&lt;/h2&gt;

&lt;p&gt;Always take a backup before shrinking/expanding or doing any configurations on your partitions. If you already have backups in your place then you are good to go, but If you don't then you can follow along from this tutorial to &lt;a href="https://akashrajpurohit.com/blog/backup-users-home-directory-in-linux-using-tar-command/"&gt;create a backup using tar command&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now if you already have enough space, you can skip the next step of shrinking any partition to get the space available. Although in my case, I did not have extra space, I had to shrink it from the home directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shrink Home
&lt;/h2&gt;

&lt;p&gt;There are some prerequisites before we start the shrinking process.&lt;/p&gt;

&lt;p&gt;To follow along, the first thing that you want to do is to switch yourself to the root user. And by switching means logging out from your non-root user and login in with username as root and your root password.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You have to go into root user because your non-root (the current user) would be mounting the home directory, so for shrinking, you would have to unmount the home partition but since some processes might be using this partition it might throw you the error like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Do you want to unmount &lt;span class="s2"&gt;"/home"&lt;/span&gt; ? &lt;span class="o"&gt;[&lt;/span&gt;Y|n] y
umount: /home: target is busy.
fsadm: Cannot proceed with mounted filesystem &lt;span class="s2"&gt;"/home"&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
  /usr/sbin/fsadm failed: 1
  Filesystem resize failed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can check if the target is busy or not using the &lt;code&gt;fuser -mv /dev/mapper/fedora_localhost--live-home&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Remember you can check where your fedora &lt;code&gt;root&lt;/code&gt; and &lt;code&gt;home&lt;/code&gt; partition is mounted using &lt;code&gt;fdisk -l&lt;/code&gt; command. In my case it is fedora_localhost--live-root and fedora_localhost--live-home)&lt;/p&gt;

&lt;p&gt;Now to shrink this partition we will use &lt;code&gt;lvresize&lt;/code&gt; tool.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;lvresize &lt;span class="nt"&gt;-L&lt;/span&gt; &lt;span class="nt"&gt;-20G&lt;/span&gt; &lt;span class="nt"&gt;--resizefs&lt;/span&gt; /dev/mapper/fedora_localhost--live-home
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will shrink 20Gb from home.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extend Root Partition
&lt;/h2&gt;

&lt;p&gt;Now you can see available space using the &lt;code&gt;vgs&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vgs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output will be like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  VG                    &lt;span class="c"&gt;#PV #LV #SN Attr   VSize     VFree&lt;/span&gt;
  fedora_localhost-live   1   3   0 wz--n- 475.35g    20g
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can add this extra space into the root using the same &lt;code&gt;lvresize&lt;/code&gt; tool. But now instead of -20G we will add +20G to root.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;lvresize &lt;span class="nt"&gt;-L&lt;/span&gt; +20G &lt;span class="nt"&gt;--resizefs&lt;/span&gt; /dev/mapper/fedora_localhost--live-root
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it, reboot your system and login with your normal non-root user and you would have your root size increased.&lt;/p&gt;

&lt;p&gt;Hope this helped you. See you in the next one.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>root</category>
      <category>partiton</category>
      <category>tooling</category>
    </item>
    <item>
      <title>Backup Users Home Directory In Linux Using Tar Command</title>
      <dc:creator>Akash Rajpurohit</dc:creator>
      <pubDate>Mon, 01 Mar 2021 12:33:59 +0000</pubDate>
      <link>https://forem.com/akashrajpurohit/backup-users-home-directory-in-linux-using-tar-command-4ehe</link>
      <guid>https://forem.com/akashrajpurohit/backup-users-home-directory-in-linux-using-tar-command-4ehe</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;We often need to do things that may put us at risk of losing data in Linux, nevertheless, it's always better to take systematic backups from time to time.&lt;/p&gt;

&lt;p&gt;Today we will be learning about how to use the powerful tool called &lt;code&gt;tar&lt;/code&gt; in Linux to help in the backup process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tar Command
&lt;/h2&gt;

&lt;p&gt;Let's take a quick look at the tar command and the flags that we will be using with it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-zcvpf&lt;/span&gt; /[Backup_Location]/[Backup_Filename] /[User_Home_Directory_Location]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's understand these flags.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;z : Compress the backup file with ‘gzip’ to make it a small size.&lt;/li&gt;
&lt;li&gt;c : Create a new backup archive.&lt;/li&gt;
&lt;li&gt;v : verbosely list files that are processed.&lt;/li&gt;
&lt;li&gt;p : Preserves the permissions of the files put in the archive for later restoration.&lt;/li&gt;
&lt;li&gt;f : use archive file or device ARCHIVE.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Let us now backup the home directory, in my case, the user name is &lt;code&gt;akash&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-zcvpf&lt;/span&gt; /backup/akash-backup-&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%d-%m-%Y&lt;span class="si"&gt;)&lt;/span&gt;.tar.gz /home/akash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will produce the output (for the /backup directory) as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-lh&lt;/span&gt; /backup

total 15G
&lt;span class="nt"&gt;-rw-r--r--&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt; 1 root root 15G Mar  1 12:09 akash-backup-01-03-2021.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you wish to exclude some directory to be archived, you can use &lt;code&gt;--exclude&lt;/code&gt; flag, something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'/home/akash/Documents/test-folder'&lt;/span&gt; &lt;span class="nt"&gt;-zcvpf&lt;/span&gt; /backup/akash-backup-&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%d-%m-%Y&lt;span class="si"&gt;)&lt;/span&gt;.tar.gz /home/akash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will archive everything excluding the &lt;code&gt;test-folder&lt;/code&gt; inside Documents.&lt;/p&gt;

&lt;p&gt;And that's it, this will help you create a backup and store it with the date inside &lt;code&gt;/backup&lt;/code&gt; directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus
&lt;/h2&gt;

&lt;p&gt;Doing this once is fine, but what if there was a way to regularly backup your content and automatically wipe out the old backups, wouldn't that be great. So let's just do that.&lt;/p&gt;

&lt;p&gt;We will use a shell script and cron job to automate this task for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backup Script
&lt;/h2&gt;

&lt;p&gt;First, let's move into a better directory where we will store our script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nano /opt/scripts/home-dir-backup.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now copy the mentioned script, it is pretty straightforward.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="nv"&gt;DATE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%d-%m-%Y&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;BACKUP_DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/backup"&lt;/span&gt;

&lt;span class="c"&gt;# To backup akash's home directory&lt;/span&gt;
&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-zcvpf&lt;/span&gt; &lt;span class="nv"&gt;$BACKUP_DIR&lt;/span&gt;/akash-&lt;span class="nv"&gt;$DATE&lt;/span&gt;.tar.gz /home/akash

&lt;span class="c"&gt;# To delete files older than 15 days&lt;/span&gt;
find &lt;span class="nv"&gt;$BACKUP_DIR&lt;/span&gt;/&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="nt"&gt;-mtime&lt;/span&gt; +15 &lt;span class="nt"&gt;-exec&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="se"&gt;\;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set executable permissions for this file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x home-dir-backup.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your script is ready to be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cron Job
&lt;/h2&gt;

&lt;p&gt;The final step is to set up the cron job to run this script automatically. We will enter into crontab using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;crontab &lt;span class="nt"&gt;-e&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside it paste the following lines at the bottom.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;0 12 &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; 5 /opt/scripts/home-dir-backup.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will run this script every Friday at 12:00. To learn more about how to configure these values, I would highly recommend using &lt;a href="https://crontab.guru/"&gt;crontab.guru&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hope you found this helpful, see you in the next one.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>tooling</category>
      <category>bash</category>
      <category>backups</category>
    </item>
    <item>
      <title>Top Free Services To Deploy Full-Stack Applications (Back-end, Front-end, Databases)</title>
      <dc:creator>Akash Rajpurohit</dc:creator>
      <pubDate>Sun, 14 Feb 2021 05:03:41 +0000</pubDate>
      <link>https://forem.com/akashrajpurohit/top-free-services-to-deploy-full-stack-applications-back-end-front-end-databases-3nmh</link>
      <guid>https://forem.com/akashrajpurohit/top-free-services-to-deploy-full-stack-applications-back-end-front-end-databases-3nmh</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;So you have developed a full-stack web application and now you are wondering how to take it online?&lt;/p&gt;

&lt;p&gt;Is it just a side project that you want to showcase or a small scale project to test it out with real users and get some feedback?&lt;/p&gt;

&lt;p&gt;One question you must be having is how can I get it done in the least cost possible right? Today we will see a list of amazing top services, using which you can easily go live with your project for &lt;strong&gt;FREE!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4trir8ognqx39krafhyg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F4trir8ognqx39krafhyg.jpg" alt="Free Hosting Meme" width="565" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will see services for Back-end, Front-end, and even Databases 🤯. In the end, I'll drop in my personal choice and views regarding the same.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note - I am not being paid or sponsored by any of these services. This is just advice based on my experience as a freelancer, and on how I host my web applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Back-End
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Heroku
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.heroku.com/"&gt;Heroku&lt;/a&gt; is a PaaS i.e Platform as a Service, you can deploy your backend applications very quickly and easily on Heroku. They have a &lt;a href="https://www.heroku.com/pricing"&gt;free tier&lt;/a&gt; where you can run up to &lt;strong&gt;100 backend apps at the same time for free&lt;/strong&gt;. One catch is that you collectively have only &lt;strong&gt;1000 free dyno/app-hours per month&lt;/strong&gt; but its more than enough for a hobby or side project.&lt;/p&gt;

&lt;p&gt;Also, Heroku has a wide &lt;a href="https://elements.heroku.com/"&gt;variety of add-ons&lt;/a&gt; which makes this entire ecosystem very easy to use and setup.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Vercel
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://vercel.com/"&gt;Vercel or Formerly known as Zeit&lt;/a&gt; is another great option to deploy both Backend and Frontend applications, It uses &lt;a href="https://en.wikipedia.org/wiki/Serverless_computing"&gt;serverless architecture&lt;/a&gt; to set up their infrastructure for deployment. Most people use vercel to deploy frontend applications but you can also deploy backend apps with vercel. This guide shows you &lt;a href="https://vercel.com/guides/deploying-a-mongodb-powered-api-with-node-and-vercel"&gt;how to deploy Node.js API with MongoDB on vercel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Vercel has a very &lt;a href="https://vercel.com/pricing"&gt;generous free tier&lt;/a&gt; with these specs&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Free&lt;/th&gt;
&lt;th&gt;Pro&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Serverless Functions Created per Deployment&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serverless Functions Created per Month&lt;/td&gt;
&lt;td&gt;160&lt;/td&gt;
&lt;td&gt;640&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serverless Function Duration (Seconds)&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  3. Firebase
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://firebase.google.com/"&gt;Firebase&lt;/a&gt; is not just a hosting service but a google backed product that provides lots of features along with hosting. Firebase gives you functionality like analytics, databases, messaging and crash reporting, etc.&lt;/p&gt;

&lt;p&gt;Along with hosting, many of the other services are also available in &lt;a href="https://firebase.google.com/pricing"&gt;free tier&lt;/a&gt; and as you scale up, you can go ahead with their Pay-as-you-Go model.&lt;/p&gt;

&lt;p&gt;You can host serverless functions called &lt;a href="https://firebase.google.com/docs/functions"&gt;Cloud Functions&lt;/a&gt;, however recently google has announced that to create functions that use Node.js version 10 and above, you have to &lt;a href="https://support.google.com/googleapi/answer/6158867?hl=en"&gt;enable billing&lt;/a&gt; for the account.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Fly.io
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://fly.io/"&gt;Fly.io&lt;/a&gt; is a relatively new platform (founded in 2017) which has a &lt;a href="https://fly.io/docs/about/pricing/#free-for-side-projects"&gt;free tier&lt;/a&gt; to test out the platform or host small side projects. They give each user \$10/mo of service credit that automatically applies to any paid service.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;RAM&lt;/th&gt;
&lt;th&gt;Monthly (approx)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;micro-1x&lt;/td&gt;
&lt;td&gt;128MB&lt;/td&gt;
&lt;td&gt;$2.67&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;micro-2x&lt;/td&gt;
&lt;td&gt;512MB&lt;/td&gt;
&lt;td&gt;$8&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can easily follow along with their &lt;a href="https://fly.io/docs/getting-started/installing-flyctl/"&gt;quickstart guide&lt;/a&gt; to set up the deployment workflow using their CLI tool.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Freemium IaaS
&lt;/h3&gt;

&lt;p&gt;I have clubbed together AWS, GCP, and Azure into this category. All three of these provide a free tier with a basic machine to set up for a year.&lt;/p&gt;

&lt;p&gt;Read more about their services in the free tier at&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://aws.amazon.com/free/"&gt;Amazon Web Services&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cloud.google.com/free/"&gt;Google Cloud Platform&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://azure.microsoft.com/en-us/free"&gt;Azure&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These all are indeed good but sometimes can become too overwhelming for beginners by looking at their vast number of services and interconnection between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Front-End
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Netlify
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.netlify.com/"&gt;Netlify&lt;/a&gt; is an amazing service for hosting you static websites, you can easily &lt;strong&gt;host unlimited projects&lt;/strong&gt;, &lt;strong&gt;invoke serverless functions&lt;/strong&gt;, &lt;strong&gt;add a custom domain with HTTPS&lt;/strong&gt;, &lt;strong&gt;Store large files&lt;/strong&gt;, &lt;strong&gt;Preview Deploys&lt;/strong&gt; and &lt;a href="https://www.netlify.com/products/"&gt;many more&lt;/a&gt; for free.&lt;/p&gt;

&lt;p&gt;Recently Netlify also released &lt;a href="https://docs.netlify.com/configure-builds/build-plugins/"&gt;Plugins Ecosystem&lt;/a&gt; which allows you to customize your build process.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Vercel
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://vercel.com/"&gt;Vercel&lt;/a&gt; as mentioned above, not only can be used to deploy backend APIs but in fact has a better ecosystem built to deploy static frontend applications. They have solutions for &lt;strong&gt;Next.js&lt;/strong&gt;, &lt;strong&gt;Gatsby&lt;/strong&gt;, &lt;strong&gt;React&lt;/strong&gt;, &lt;strong&gt;Vue&lt;/strong&gt;, &lt;strong&gt;Angular&lt;/strong&gt;, &lt;strong&gt;Nuxt&lt;/strong&gt; out of the box with &lt;strong&gt;Zero Configuration&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Vercel also provides Edge Network or &lt;a href="https://en.wikipedia.org/wiki/Content_delivery_network"&gt;Content Delivery Network&lt;/a&gt; and has multiple layers of caches to ensure that your Vercel deployments are fast around the world. You can read more about it on their &lt;a href="https://vercel.com/docs/introduction"&gt;docs section&lt;/a&gt; which in my opinion is nicely structured for better understanding.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Github Pages
&lt;/h3&gt;

&lt;p&gt;If you are a heavy user of GitHub and just want to quickly have a deployed version of your project then &lt;a href="https://pages.github.com/"&gt;Github Pages&lt;/a&gt; is the best choice for you. Github Pages host your code directly from your GitHub repository, so any changes/edits you make and push it to the repository are automatically pushed to your live website.&lt;/p&gt;

&lt;p&gt;Follow this &lt;a href="https://docs.github.com/en/github/working-with-github-pages"&gt;amazing guide by github&lt;/a&gt; to set up your next project using Github Pages.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Surge.sh
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://surge.sh/"&gt;Surge.sh&lt;/a&gt; is one of the simplest static site publishing providers. You can use their CLI tool to publish any project directly from the terminal or you could use its integration with various build tools like &lt;a href="https://surge.sh/help/deploying-a-grunt-project"&gt;Grunt&lt;/a&gt;, &lt;a href="https://github.com/surge-sh/gulp-surge"&gt;Gulp&lt;/a&gt;, &lt;a href="https://surge.sh/help/integrating-with-travis-ci"&gt;Travis CI&lt;/a&gt; and many more.&lt;/p&gt;

&lt;p&gt;Try out surge &lt;a href="https://surge.sh/pricing"&gt;free tier&lt;/a&gt; which is more than enough for any small to medium size front end application.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Render
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://render.com/"&gt;Render&lt;/a&gt; is another great service to host your both front end and back end applications and database, Although they don't have any free tier for back end and databases (&lt;em&gt;as of now&lt;/em&gt;) but they have a really good &lt;a href="https://render.com/pricing"&gt;free tier for static web hosting&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The services are more or less similar to the other services mentioned above, so in the end, it all depends on your experience as a developer using these services.&lt;/p&gt;

&lt;h2&gt;
  
  
  Database
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. MongoDB Atlas
&lt;/h3&gt;

&lt;p&gt;The company &lt;a href="https://www.mongodb.com/"&gt;MongoDB&lt;/a&gt; itself created this service called &lt;a href="https://www.mongodb.com/cloud/atlas"&gt;MongoDB Atlas&lt;/a&gt; and provides a really good free tier with &lt;strong&gt;512 MB of Storage&lt;/strong&gt;, &lt;strong&gt;Shared RAM&lt;/strong&gt; and &lt;strong&gt;Highly available replica sets, end-to-end encryption, automated patches, REST API&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Previously, I was using &lt;a href="https://mlab.com/"&gt;mLab&lt;/a&gt; as Database-as-a-Service for most of my projects, but a few months back mLab was &lt;a href="https://blog.mlab.com/2018/10/mlab-is-becoming-a-part-of-mongodb-inc/"&gt;acquired by MongoDB&lt;/a&gt; and for the greater good, as MongoDB provides additional services and better security rules for your shared MongoDB Databases. There is no doubt that Atlas is the best Cloud Database you can get for rapid prototyping and small to medium-sized projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Elephant SQL
&lt;/h3&gt;

&lt;p&gt;When I am not using a NoSQL database like MongoDB and need an RDBMS database for any project, I mostly use &lt;a href="https://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt;. &lt;a href="https://www.elephantsql.com/"&gt;Elephant SQL&lt;/a&gt; is a great PostgreSQL as a Service for the cloud. Although they have a variety of plans for their service but their &lt;a href="https://www.elephantsql.com/plans.html"&gt;free tier&lt;/a&gt; is great for getting started with the PostgreSQL database in the cloud.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Heroku Postgres
&lt;/h3&gt;

&lt;p&gt;As I have mentioned above in the Heroku section about the additional add-ons that Heroku provides, one of them is &lt;a href="https://www.heroku.com/postgres"&gt;Heroku Postgres&lt;/a&gt;. This is what I was using previously before I discovered about elephantSQL and I am pretty much satisfied with the services that Heroku Postgres provides.&lt;/p&gt;

&lt;p&gt;Moreover, it's not just great but also completely free, you don't even need to add credit card details to verify your account to use this (as for some other service like &lt;a href="https://www.heroku.com/redis"&gt;Heroku Redis&lt;/a&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Redis Lab
&lt;/h3&gt;

&lt;p&gt;Most of my side/client projects involve tasks where I require a fast, in-memory datastore either for caching, &lt;a href="https://akashrajpurohit.com/blog/api-rate-limit-in-nodejs-and-expressjs/"&gt;rate limiting&lt;/a&gt; or session management, for that, I mostly go with &lt;a href="https://redis.io/"&gt;Redis&lt;/a&gt;. For the cloud Redis database, I use &lt;a href="https://redislabs.com/"&gt;Redis Lab&lt;/a&gt;. You can signup for an account and get 1 database with 30 MB storage for free. This is more than enough for me for prototyping and testing things out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So these were most of the services that I have personally used and would recommend to others as well. My final thoughts are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I mostly use Heroku and Vercel for deploying most of my backend applications&lt;/li&gt;
&lt;li&gt;For frontend I mostly choose between Netlify and Vercel from the mentioned options, but all of these options provide very similar kind of services so at the end it boils down to our own experience while using them, my suggestion would be to try each one out and choose for yourself which is your preference&lt;/li&gt;
&lt;li&gt;For Databases, I have laid down all the services I use for different types of databases, there may be other service providers but these are the ones I tend to choose.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let me know if something is missing in the list which you would've liked to see. 😊&lt;/p&gt;

</description>
      <category>resources</category>
      <category>services</category>
      <category>devops</category>
      <category>fullstack</category>
    </item>
    <item>
      <title>API Rate Limit for Small Size to Large Production Ready Applications in Node.js</title>
      <dc:creator>Akash Rajpurohit</dc:creator>
      <pubDate>Fri, 12 Feb 2021 07:33:08 +0000</pubDate>
      <link>https://forem.com/akashrajpurohit/api-rate-limit-for-small-size-to-large-production-ready-applications-in-node-js-3gld</link>
      <guid>https://forem.com/akashrajpurohit/api-rate-limit-for-small-size-to-large-production-ready-applications-in-node-js-3gld</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Rate limiting is one of the most important security feature that you must add for securing backend APIs from malicious attacks like brute-forcing &lt;code&gt;/login&lt;/code&gt; or &lt;code&gt;/admin&lt;/code&gt; and for handling unwanted flood of requests from users. In simple terms, rate-limiting allows us as a developer to control the rate at which user requests are processed by our server.&lt;/p&gt;

&lt;p&gt;In this guide, we will learn how to add rate limiting for a small-sized project as quickly as possible and how to scale such a method for a production-ready application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do you want to add rate-limiting?
&lt;/h2&gt;

&lt;p&gt;Simply said, you want to reduce the risk of &lt;a href="https://en.wikipedia.org/wiki/DOS"&gt;DOS&lt;/a&gt; attacks and make sure that your server is never overloaded.&lt;/p&gt;

&lt;p&gt;For example, you have to build a Public API and you want to allow unsubscribed users to make only 100 requests per hour. Once the user has exceeded that limit, you just want to simply ignore the request and send them an error indicating that they have exhausted their free limit and consider to subscribe for your API or anything or that sort.&lt;/p&gt;

&lt;p&gt;Kindly bear in mind that for implementing a rate-limiting technique, there must be a clearly defined constraint, which could be based on any of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;b&gt;IP address:&lt;/b&gt; The constraint is on the IP address of the device from where the request was initiated.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;Location:&lt;/b&gt; The constraint here is based on the geographical region which is implemented based
on the location from where the request was made.&lt;/li&gt;
&lt;li&gt;
&lt;b&gt;Users: &lt;/b&gt; The constraint is made on the particular user and is implemented by using a unique identifier
for the particular user like userId, API KEY, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Several algorithms can be used to implemented rate limiting, you can &lt;a href="https://en.wikipedia.org/wiki/Rate_limiting"&gt;read more about them here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Given that, let us start with the practical implementation of API rate limiting.&lt;/p&gt;

&lt;h2&gt;
  
  
  For Small &amp;amp; Medium-Sized Applications
&lt;/h2&gt;

&lt;p&gt;We will use a third-party npm package called &lt;a href="https://www.npmjs.com/package/express-rate-limit"&gt;express-rate-limit&lt;/a&gt; for this purpose. Surely we can build a custom middleware ourselves but there is no need to reinvent the wheel.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Setup Basic Project
&lt;/h3&gt;

&lt;p&gt;I'm assuming that you already have a project set up with express. If not then quickly setup an express project using &lt;a href="https://www.npmjs.com/package/boilerplate-gen"&gt;boilerplate-gen&lt;/a&gt; package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx boilerplate-gen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and choose express for the project template.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Install the third party package
&lt;/h3&gt;

&lt;p&gt;Install the package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add express-rate-limit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Create the rate limit middleware
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rateLimit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express-rate-limit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Rate limit middleware&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rateLimitMiddleware&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rateLimit&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;windowMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You have exceeded your 100 requests per hour limit.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Export it&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rateLimitMiddleware&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's now quickly try to understand what we are doing here.&lt;/p&gt;

&lt;p&gt;We are exporting a function called &lt;code&gt;rateLimitMiddleware&lt;/code&gt; which invokes the rateLimit function which we installed from the package. This middleware enforces rate limiting based on the options we have passed in, which are -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;windowMs&lt;/code&gt; - The window size in milliseconds, which in our case is 1 hour.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;max&lt;/code&gt; - Maximum number of requests which can be allowed in the given window size.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;message&lt;/code&gt; - The error message that the user would get when they exceed their limit.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;headers&lt;/code&gt; - This option automatically adds the appropriate headers showing that this API resource is rate limited (&lt;code&gt;X-RateLimit-Limit&lt;/code&gt;), current usage (&lt;code&gt;X-RateLimit-Remaining&lt;/code&gt;) and time to wait before retrying (&lt;code&gt;Retry-After&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we have created our middleware, we have simply configure our application to use this middleware when handling the requests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Use the middleware
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rateLimitMiddleware&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./middlewares/ratelimit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Use Ratelimit Middleware&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rateLimitMiddleware&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voilà! We are done here. Now all the requests will be rate-limited according to your configurations. Also you can add multiple middlewares with different sets of configurations for certain routes.&lt;/p&gt;

&lt;p&gt;For eg, normal routes can be rate-limited to 100 requests per hour and &lt;code&gt;/login&lt;/code&gt; or &lt;code&gt;/admin&lt;/code&gt; can be rate-limited to 20 requests per hour to avoid &lt;a href="https://en.wikipedia.org/wiki/Brute-force_attack"&gt;brute force password attacks&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: This package identifies users by their IP addresses using &lt;code&gt;req.ip&lt;/code&gt; by default&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Great! So now you have added a rate limiter for your API with just 4 simple steps.&lt;/p&gt;

&lt;p&gt;Now let's move to the other half section of this blog, which is...&lt;/p&gt;

&lt;h2&gt;
  
  
  For Large Applications
&lt;/h2&gt;

&lt;p&gt;The above-mentioned implementation is really good if you are building a small to medium size application. However, this approach will not scale for large applications.&lt;/p&gt;

&lt;p&gt;Why is that?? You must be asking right.&lt;/p&gt;

&lt;p&gt;Firstly, if your application size is large, most likely you won't be having a single node process on a single server. Rather you will have multiple node processes running on a distributed system and the above third party package does not share state with other processes/servers by default.&lt;/p&gt;

&lt;p&gt;So using the same configurations you won't be able to scale.&lt;/p&gt;

&lt;p&gt;So what's the solution there? How can I share the state between multiple server instances?&lt;/p&gt;

&lt;p&gt;The answer is quite simple&lt;/p&gt;

&lt;p&gt;You use an &lt;code&gt;External Data Store&lt;/code&gt; to store all the information.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;express-rate-limit&lt;/code&gt; package uses &lt;code&gt;Memory Store&lt;/code&gt; by default which stores &lt;code&gt;hits&lt;/code&gt; in-memory in the Node.js process, hence cannot share the state between processes.&lt;/p&gt;

&lt;p&gt;So we can use an external data store to store this information and that way we can have multiple processes/servers using that external store and that way we can scale our application.&lt;/p&gt;

&lt;p&gt;Now the question is what should we use as our &lt;code&gt;Data Store&lt;/code&gt;. Well, there are many choices like -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Redis Store&lt;/li&gt;
&lt;li&gt;Memcached Store&lt;/li&gt;
&lt;li&gt;Mongo Store&lt;/li&gt;
&lt;li&gt;PostgreSQL&lt;/li&gt;
&lt;li&gt;MySQL etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would prefer to choose &lt;a href="https://redis.io/"&gt;Redis&lt;/a&gt; because it is fast and flexible with support for various types of data structures.&lt;/p&gt;

&lt;p&gt;Also we will use another third-party package called &lt;a href="https://www.npmjs.com/package/rate-limiter-flexible"&gt;rate-limiter-flexible&lt;/a&gt; as it works with Redis, process Memory, Cluster or PM2, Memcached, MongoDB, MySQL, PostgreSQL and allows to control requests rate in a single process or distributed environment.&lt;/p&gt;

&lt;p&gt;Let's now start with the implementation part. Assuming you have an existing project, or use the same method from above to set up a new one quickly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Install the package
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add rate-limiter-flexible redis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Setup the middleware
&lt;/h3&gt;

&lt;p&gt;We will use Redis for the project, if you don't have Redis installed then first &lt;a href="https://redis.io/download"&gt;download and install it from here&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redis&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;RateLimiterRedis&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rate-limiter-flexible&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Create redis client&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;redisClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redis&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;6379&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Setup Rate Limiter&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rateLimiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RateLimiterRedis&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;redisClient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// redis client instance&lt;/span&gt;
  &lt;span class="na"&gt;keyPrefix&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;appname:rl&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// prefix your keys with some name&lt;/span&gt;
  &lt;span class="na"&gt;points&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 10 requests&lt;/span&gt;
  &lt;span class="na"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// per 1 second by IP&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Setup the middleware using the rate limiter config&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rateLimiterMiddleware&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// On the basis of ip address, but can be modified according to your needs&lt;/span&gt;
  &lt;span class="nx"&gt;rateLimiter&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;consume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;429&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Too Many Requests&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rateLimiterMiddleware&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break it down by each section.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We import the packages, both Redis and rate-limiter-flexible, and use the &lt;code&gt;RateLimiterRedis&lt;/code&gt; since we are implementing with Redis.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We create Redis client connecting to the local machine on Redis default port &lt;code&gt;6379&lt;/code&gt;. You can use a remote hosted machine with Redis as well here (which you might do for large systems).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We create the rateLimiter instance with some configuration options&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;redis&lt;/code&gt; - The redisClient instance we created.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;keyPrefix&lt;/code&gt; - Adding a prefix to all the keys generated from it, we can use it like &lt;code&gt;appname:rl&lt;/code&gt;, rl is for ratelimit. Feel free to choose any other keyPrefix.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;points&lt;/code&gt; - Maximum number of points can be consumed over the duration.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;duration&lt;/code&gt; - Number of seconds before consumed points are reset. If it is set to &lt;code&gt;0&lt;/code&gt; then it never resets.&lt;/p&gt;

&lt;p&gt;You can view more &lt;a href="https://www.npmjs.com/package/rate-limiter-flexible#basic-options"&gt;options here&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Finally, we set up our middleware which uses the rateLimiter instance we created above and we consume based on the &lt;code&gt;req.ip&lt;/code&gt; i.e IP address of the user.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, we will use this middleware in our &lt;code&gt;app.js&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Use the middleware
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rateLimiterRedisMiddleware&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./middleware/rateLimiterRedis&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rateLimiterRedisMiddleware&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And..that's it. Now you can scale your application with rate limiter. I would highly suggest you to check the docs of the package for more details and configuration options.&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;We learn how to set up rate limiter for nodejs and expressjs for small and large size applications with implementation with code examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Rate_limiting"&gt;What is Rate Limiting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/express-rate-limit"&gt;express-rate-limit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.npmjs.com/package/rate-limiter-flexible"&gt;rate-limiter-flexible&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://redis.io/"&gt;Redis&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>api</category>
      <category>ratelimit</category>
    </item>
    <item>
      <title>Disable Source Maps in GatsbyJs V2 for Production</title>
      <dc:creator>Akash Rajpurohit</dc:creator>
      <pubDate>Fri, 12 Feb 2021 07:19:10 +0000</pubDate>
      <link>https://forem.com/akashrajpurohit/disable-source-maps-in-gatsbyjs-v2-for-production-bla</link>
      <guid>https://forem.com/akashrajpurohit/disable-source-maps-in-gatsbyjs-v2-for-production-bla</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;GatsbyJS in version 2 automatically includes source maps when you build your site for production using the &lt;code&gt;yarn build&lt;/code&gt; command. This is a really handy feature that enables you to &lt;a href="https://developer.mozilla.org/en-US/docs/Tools/Debugger/How_to/Use_a_source_map" rel="noopener noreferrer"&gt;debug and view your code in production&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If your website or app is open source then you would not mind people being able to view your code directly when visiting your site, but if your code is a private source or some client projects for which you don't want to explicitly allow others to view your code, you would have to disable the generation of source maps when gatsby builds your site.&lt;/p&gt;

&lt;p&gt;In this blog, we will see how to configure source maps settings for your gatsbyjs v2 websites i.e have source maps in development so that your life is a bit easy as a developer but disable them on production builds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Customize Webpack
&lt;/h2&gt;

&lt;p&gt;GatsbyJs uses &lt;a href="https://webpack.js.org/" rel="noopener noreferrer"&gt;Webpack&lt;/a&gt; for bundling the files, don't worry if you don't know how to configure webpack because for this task we won't be doing any high-level configurations.&lt;/p&gt;

&lt;p&gt;What we have to do is disable the &lt;code&gt;devtool&lt;/code&gt; flag when we are in the production environment.&lt;/p&gt;

&lt;p&gt;So, since now we know what we have to do, let's take a look at how we are going to do it.&lt;/p&gt;

&lt;p&gt;We can modify the configuration options for webpack in gatsby by exporting a function called &lt;code&gt;onCreateWebpackConfig&lt;/code&gt; in the &lt;code&gt;gatsby-node.js&lt;/code&gt; file. If you don't have a gatsby-node.js file then create a new one (most likely you would already be having that at the root of your folder).&lt;/p&gt;

&lt;p&gt;Copy the snippet below in the gatsby-node.js file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onCreateWebpackConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getConfig&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getConfig&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Turn off source maps in production&lt;/span&gt;
    &lt;span class="nx"&gt;actions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setWebpackConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;devtool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  What's Happening
&lt;/h2&gt;

&lt;p&gt;So first we are destructing &lt;code&gt;actions&lt;/code&gt; and &lt;code&gt;getConfig&lt;/code&gt; from this function from the &lt;a href="https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/#onCreateWebpackConfig" rel="noopener noreferrer"&gt;list of available configurations&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We will first get the current config by calling the getConfig function, it has a lot of properties but the ones in which we are interested are devtool and mode.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="nx"&gt;devtool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;source-map&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;when NODE_ENV is set to production, we would get the mode value set to production in the config as well, so we would first check if this mode is production or not.&lt;/p&gt;

&lt;p&gt;If it is production then we would call the method &lt;code&gt;setWebpackConfig&lt;/code&gt; on the actions object and pass the devtool option to false (which is initially set to 'source-map').&lt;/p&gt;

&lt;p&gt;And that's it, we have successfully disabled source maps for our website in production.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: GatsbyJS creates the public directory and it is &lt;em&gt;usually&lt;/em&gt; not cleared automatically when you are running &lt;code&gt;gatsby build&lt;/code&gt;, so to check the effects you have to clear the public and .cache directory (use &lt;code&gt;gatsby clean&lt;/code&gt; command). You can add these scripts to your &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;br&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;build&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gatsby build --log-pages&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;br&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;clean&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;gatsby clean&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;br&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;br&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Pro Tip for Netlify&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;If you are hosting your site on &lt;a href="https://www.netlify.com/" rel="noopener noreferrer"&gt;Netlify&lt;/a&gt; then to see the changes, you have to clear the cache and deploy the site again. You can find this option on your dashboard at &lt;a href="https://app.netlify.com/sites/SITE_NAME/deploys" rel="noopener noreferrer"&gt;https://app.netlify.com/sites/SITE_NAME/deploys&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F90z61rg3134pb4ivjljk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F90z61rg3134pb4ivjljk.png" alt="Clear cache on Netlify"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;We learn how to disable source maps in production for the GatsbyJS version 2 website.&lt;/p&gt;

&lt;h2&gt;
  
  
  More reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.html5rocks.com/en/tutorials/developertools/sourcemaps/" rel="noopener noreferrer"&gt;What is source maps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Tools/Debugger/How_to/Use_a_source_map" rel="noopener noreferrer"&gt;Use of source maps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://css-tricks.com/should-i-use-source-maps-in-production/" rel="noopener noreferrer"&gt;Should I Use Source Maps in Production?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See you in the next one. 👋&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>sourcemaps</category>
    </item>
  </channel>
</rss>
