<?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>DEV Community: XinYang Yu</title>
    <description>The latest articles on DEV Community by XinYang Yu (@xinyangyxy).</description>
    <link>https://dev.to/xinyangyxy</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%2F279463%2F653e8f85-2bb9-4baa-be87-eecd06e5cdaa.jpeg</url>
      <title>DEV Community: XinYang Yu</title>
      <link>https://dev.to/xinyangyxy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xinyangyxy"/>
    <language>en</language>
    <item>
      <title>How to create Environment Variable on Windows &amp; use it inside ASP.NET Web Form</title>
      <dc:creator>XinYang Yu</dc:creator>
      <pubDate>Mon, 28 Dec 2020 05:26:40 +0000</pubDate>
      <link>https://dev.to/xinyangyxy/how-to-create-environment-variable-on-windows-use-it-inside-asp-net-web-form-36l6</link>
      <guid>https://dev.to/xinyangyxy/how-to-create-environment-variable-on-windows-use-it-inside-asp-net-web-form-36l6</guid>
      <description>&lt;p&gt;ASP.NET Web Form may not be as sexy as those new web stacks such as MERN and JAM stack. But Web Form is still widely used in many enterprise-level systems.&lt;/p&gt;

&lt;p&gt;In this short article, I will walk you through how to implement environment variables inside Web Form on Windows. This can come handy if you open source your Web Form project. Preventing your precious secrets from leaking to strangers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create an Environment Variable in Windows
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open System panel &amp;amp; Click on &lt;strong&gt;Advanced system settings&lt;/strong&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GuhO5oPm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u9kwhndvvpcqt3l4dnyc.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Navigate to the &lt;strong&gt;Advanced&lt;/strong&gt; tab &amp;amp; click on &lt;strong&gt;Environment Variables&lt;/strong&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RZTSEkG1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3c0pxjt2f27r34cy77lm.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Click on the &lt;strong&gt;New&lt;/strong&gt; button to create an environment variable
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2DbnZ4ko--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n5ic4zqbutvk8916zw4n.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;In this case, the variable name is &lt;strong&gt;Hello&lt;/strong&gt;, the variable value is &lt;strong&gt;World&lt;/strong&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g3MJzj18--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8pmu0ke6cpz1u7m12tdy.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;ok&lt;/strong&gt; to save
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pxz2wMi8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/joxly43y4xls2qr6negb.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Great, you have just created an environment variable on Windows!&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Using the environment variable inside ASP.NET Web Form
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;[Attention]&lt;/strong&gt; Restart your Visual Studio. Otherwise, you will face an error like below
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fsXwqc1i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mo18v6w0t9g8uiozqii5.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;In this case, we will retrieve the value from the environment variable, and insert it to a label component
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KZP2mnFH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ipa71g96q3ww2r3il11s.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;Start the application and navigate to the page where the label is located. You should be able to see the value of the variable be displayed!
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0GuqoHqp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n6nje23o721c8lyejccm.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Nice! There is all you need to do, in order to &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create an environment variable on Windows&lt;/li&gt;
&lt;li&gt;Use the environment variables inside ASP.NET Web Form&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for taking time to read this article. If you have any questions or I missed out something important. Please feel free to comment below. Stay hacking! :)&lt;/p&gt;

</description>
      <category>windows</category>
      <category>environmentvariable</category>
      <category>net</category>
      <category>webform</category>
    </item>
    <item>
      <title>Using Environment Variables in NodeJS, and why we want to use them.</title>
      <dc:creator>XinYang Yu</dc:creator>
      <pubDate>Mon, 13 Jul 2020 15:33:25 +0000</pubDate>
      <link>https://dev.to/xinyangyxy/using-environment-variables-in-nodejs-and-why-we-want-to-use-them-1i14</link>
      <guid>https://dev.to/xinyangyxy/using-environment-variables-in-nodejs-and-why-we-want-to-use-them-1i14</guid>
      <description>&lt;p&gt;Environment variables are basically variables you stored on your own machines, instead stated directly in your source codes.&lt;/p&gt;

&lt;p&gt;You can think it in this way, instead of stating a variable in the source codes like the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;XinYang-YXY&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can store the variable and its value on your own machine and import the variable to your source codes like the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But why we want to do so? Isn't it an extra step? One simple but critical reason is SECURITY. &lt;/p&gt;

&lt;p&gt;Imagine uploading your source codes to Github and open source it. People around the world can know all your API keys easily by just looking through your source codes. &lt;/p&gt;

&lt;p&gt;If they are malicious, they can use your API keys for their own usage. And you have to pay for all the requests they generated using your API keys. You definitely don't want that to happen to you.&lt;/p&gt;

&lt;p&gt;That is why you want to use environment variables. When your source codes running locally, it is able to use import codes to import the API key value into your source codes and carry out the task. &lt;/p&gt;

&lt;p&gt;When the source codes are uploaded to Github, people can only see the import codes and don't know what is your API key. Isn't it amazing?&lt;/p&gt;

&lt;p&gt;Now I will teach you how to use environment variables inside your NodeJS application.&lt;/p&gt;

&lt;p&gt;First, you will need to install an npm package called 'dotenv'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install dotenv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Second, you need to create a dotfile aka a hidden file called '.env' at the root of your application. This is the file where you place all your environment variables - all your little secrets.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GMb0V3bf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2qyy2i5001c6qhli6e86.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GMb0V3bf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2qyy2i5001c6qhli6e86.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Third, you can place state all your environment variables inside the dotfile like the following&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nOEnkFg6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1neyckub1zy4vrhyuhrs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nOEnkFg6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1neyckub1zy4vrhyuhrs.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, it is time to import the environment variables into your source codes! Open the file you need to use the values stored in the environment variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dotenv&lt;/span&gt;&lt;span class="dl"&gt;"&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="c1"&gt;// Import the .env file you created at the root of the project&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="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="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DBHOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// same as host: "mydb.com"&lt;/span&gt;
    &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DBNAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// same as host: "db"&lt;/span&gt;
    &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DBUSER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// same as host: "admin"&lt;/span&gt;
    &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DBPASS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// same as host: "mypassword"&lt;/span&gt;

&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When you upload your source codes to Github, the '.env' file is by default not uploaded. So all your little secretes can stay private!&lt;/p&gt;

&lt;p&gt;Well done! That is all you need to do to create and use environment variables in your NodeJS project. Feel free to comment below if you have any questions. Have fun hacking!&lt;/p&gt;

</description>
      <category>node</category>
      <category>environment</category>
      <category>security</category>
      <category>variables</category>
    </item>
  </channel>
</rss>
