<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ian Poston on Medium]]></title>
        <description><![CDATA[Stories by Ian Poston on Medium]]></description>
        <link>https://medium.com/@ianposton?source=rss-f54ad2f332cd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*U_xtxFvCq8xyqwGvqV7sGA.png</url>
            <title>Stories by Ian Poston on Medium</title>
            <link>https://medium.com/@ianposton?source=rss-f54ad2f332cd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 09:10:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ianposton/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deploy create-next-app to Netlify]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://ianposton.medium.com/deploy-create-next-app-to-netlify-c3c0599a338c?source=rss-f54ad2f332cd------2"><img src="https://cdn-images-1.medium.com/max/1388/0*pHP-YhR4BGIcn2Or.png" width="1388"></a></p><p class="medium-feed-snippet">A GitHub account is required to follow along with this tutorial. NPM and node.js version 14.4 must be installed globally. This was&#x2026;</p><p class="medium-feed-link"><a href="https://ianposton.medium.com/deploy-create-next-app-to-netlify-c3c0599a338c?source=rss-f54ad2f332cd------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://ianposton.medium.com/deploy-create-next-app-to-netlify-c3c0599a338c?source=rss-f54ad2f332cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3c0599a338c</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[netlify]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Ian Poston]]></dc:creator>
            <pubDate>Fri, 25 Jun 2021 17:32:37 GMT</pubDate>
            <atom:updated>2021-06-25T17:32:37.637Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[React Next.js SSR and AlanAI | Podcast Search Netlify App Tutorial]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://ianposton.medium.com/react-next-js-ssr-and-alanai-podcast-search-netlify-app-tutorial-b21f4c70269?source=rss-f54ad2f332cd------2"><img src="https://cdn-images-1.medium.com/max/1264/0*JaR6hKF8XNI0OLRd.png" width="1264"></a></p><p class="medium-feed-snippet">React Next.js Podcast Search &#x2014; Demo</p><p class="medium-feed-link"><a href="https://ianposton.medium.com/react-next-js-ssr-and-alanai-podcast-search-netlify-app-tutorial-b21f4c70269?source=rss-f54ad2f332cd------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://ianposton.medium.com/react-next-js-ssr-and-alanai-podcast-search-netlify-app-tutorial-b21f4c70269?source=rss-f54ad2f332cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/b21f4c70269</guid>
            <category><![CDATA[netlify]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Ian Poston]]></dc:creator>
            <pubDate>Wed, 28 Apr 2021 21:49:00 GMT</pubDate>
            <atom:updated>2021-04-28T21:49:00.914Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Part 2: Angular 10 Movie Search TMDb API]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://ianposton.medium.com/part-2-angular-10-movie-search-tmdb-api-3b186aaba715?source=rss-f54ad2f332cd------2"><img src="https://cdn-images-1.medium.com/max/1264/0*RHAIfmsdymzALHAp.png" width="1264"></a></p><p class="medium-feed-snippet">Angular Universal Movie Search&#x200A;&#x2014;&#x200A;Demo</p><p class="medium-feed-link"><a href="https://ianposton.medium.com/part-2-angular-10-movie-search-tmdb-api-3b186aaba715?source=rss-f54ad2f332cd------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://ianposton.medium.com/part-2-angular-10-movie-search-tmdb-api-3b186aaba715?source=rss-f54ad2f332cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b186aaba715</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Ian Poston]]></dc:creator>
            <pubDate>Tue, 29 Sep 2020 05:42:35 GMT</pubDate>
            <atom:updated>2021-07-06T23:31:50.323Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Angular 10 Movie Search (ssr ngUniversal), TMDB API]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://ianposton.medium.com/angular-10-movie-search-ssr-nguniversal-tmdb-api-83796845dd8f?source=rss-f54ad2f332cd------2"><img src="https://cdn-images-1.medium.com/max/2560/1*R6uPoy42bneIRDHxvvPrYQ.png" width="2560"></a></p><p class="medium-feed-snippet">Angular Universal Movie Search&#x200A;&#x2014;&#x200A;Demo</p><p class="medium-feed-link"><a href="https://ianposton.medium.com/angular-10-movie-search-ssr-nguniversal-tmdb-api-83796845dd8f?source=rss-f54ad2f332cd------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://ianposton.medium.com/angular-10-movie-search-ssr-nguniversal-tmdb-api-83796845dd8f?source=rss-f54ad2f332cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/83796845dd8f</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Ian Poston]]></dc:creator>
            <pubDate>Fri, 04 Sep 2020 06:40:24 GMT</pubDate>
            <atom:updated>2021-07-06T23:13:34.660Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Angular 9 and MySportsFeeds API Part 2]]></title>
            <link>https://ianposton.medium.com/angular-9-and-mysportsfeeds-api-part-2-b317b0b54b83?source=rss-f54ad2f332cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/b317b0b54b83</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[fantasy-sports]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[sports]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Ian Poston]]></dc:creator>
            <pubDate>Fri, 03 Apr 2020 22:10:15 GMT</pubDate>
            <atom:updated>2020-04-04T03:59:38.012Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sort data for ui with a custom angular pipe</h3><p>This is part 2 of <a href="https://www.ianposton.com/angular-9-and-mysportsfeed-api-part-1/">Angular 9 and MySportsFeeds API Part 1</a>. We left off with a very long list of NBA players with a large player image. I would like to make the list shorter, more readable and put it in a proper order that makes sense.</p><p>Things TODO</p><ul><li>Sort the order of the list</li><li>Show top 20</li><li>Paginate next 20</li><li>Style the list</li></ul><p>To help me sort the players by their point totals I am going to use <a href="https://cli.angular.io/">angular cli</a> to generate a pipe. I can add some code in the pipe that will sort my data from most points to least. Then I will add the pipe to the *ngFor directive in the html. With angular cli I can run ng g pipe orderby in the root of my angular app. I enhance the html by adding the pipe &lt;div *ngFor=&quot;let item of nbaData | orderBy: &#39;nbaPts&#39;; let i=index&quot;&gt;. I am using the index value to show the number order in the list. The index is the number position of the item in the array.</p><p>I want to make the list shorter and only show the top 20 players. There are a few ways to do this, but I am using [ngClass] to pull this off. First I’ll create a css class .dn {display: none;} and then update the html &lt;span [ngClass]=&quot;{&#39;content&#39; : i&lt;=19, &#39;dn&#39; : i&gt;19}&quot;&gt;. I am wrapping the player info with a &lt;span&gt; element. The index is binary so if the index is less than or equal to 19 apply a class name to show content and if the index is greater than 19 apply a class name that will hide the content. This will show the top 20 players and hide the rest.</p><p>It would be nice to show more players that come after the top 20. I can define some variables in the controller and use a button click event to paginate next. The default will be public page: number = 19; and public amount: number = -1; to show the top 20. I can use a button to change the values on click &lt;button (click)=&quot;page = 19; amount = -1&quot;&gt;1 - 20&lt;/button&gt; &amp;nbsp;&amp;nbsp; &lt;button (click)=&quot;page = 39; amount = 19&quot;&gt;21 - 40&lt;/button&gt;. This will allow me to toggle between 1 – 20 to 21 – 40.</p><pre>//orderby.pipe.ts</pre><pre>import { Pipe, PipeTransform } from &#39;@angular/core&#39;;</pre><pre>@Pipe({name: &#39;orderBy&#39;, pure: false})<br>export class OrderBy implements PipeTransform {<br> <br>  transform(array: any[], field: string): any[] {<br>    array.sort((a: any, b: any) =&gt; {<br>      if (field === &#39;nbaPts&#39;) {<br>          if (a[&#39;stats&#39;].offense.pts &gt;= b[&#39;stats&#39;].offense.pts) {<br>            return -1;<br>          } else if (a[&#39;stats&#39;].offense.pts &lt;= b[&#39;stats&#39;].offense.pts) {<br>            return 1;<br>          } else {<br>            return 0;<br>          }<br>      }<br>    })<br>  }</pre><pre>}</pre><pre>//app.component.ts </pre><pre>import { Component, OnInit } from &#39;@angular/core&#39;;<br>import { HttpClient, HttpResponse, HttpHeaders, HttpRequest} from &#39;@angular/common/http&#39;;</pre><pre>let headers = new HttpHeaders().set(&quot;Authorization&quot;, &quot;Basic &quot; + btoa(apiToken + &quot;:&quot; + &#39;MYSPORTSFEEDS&#39;));<br>let url = &#39;https://api.mysportsfeeds.com/v2.1/pull/nba/2019-2020-regular/player_stats_totals.json?position=PG,SG,SF,PF,C&#39;;</pre><pre>@Component({<br>  selector: &#39;app-root&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.scss&#39;]<br>})</pre><pre>export class AppComponent implements OnInit {<br>   <br>   public nbaData: Array&lt;any&gt;;<br>   public page: number = 19;<br>   public amount: number = -1;</pre><pre>   constructor(private http: Http) {}</pre><pre>   loadData() {<br>    this.http.get(url, {headers})<br>      .subscribe(res =&gt; {<br>        console.log(res[&#39;playerStatsTotals&#39;], &#39;NBA players and stats&#39;);<br>        this.nbaData = res[&#39;playerStatsTotals&#39;].filter(<br>          player =&gt; player.stats != null &amp;&amp; player.stats.gamesPlayed &gt; 5);<br>      });<br>   }</pre><pre>   ngOnInit() {<br>    loadData();<br>   }<br>}</pre><pre>//app.component.html</pre><pre>&lt;div&gt;<br>  &lt;button (click)=&quot;page = 19; amount = -1&quot;&gt;1 - 20&lt;/button&gt; &amp;nbsp;&amp;nbsp; &lt;button (click)=&quot;page = 39; amount = 19&quot;&gt;21 - 40&lt;/button&gt;<br>&lt;/div&gt;</pre><pre> &lt;div class=&quot;card&quot; *ngIf=&quot;nbaData != null&quot;&gt;<br>   &lt;h2&gt;Points&lt;/h2&gt;<br>   &lt;div *ngFor=&quot;let item of myData | orderBy: &#39;nbaPts&#39;; let i=index&quot;&gt;<br>     &lt;span [ngClass]=&quot;{&#39;content&#39; : i&lt;=page, &#39;dn&#39; : i&gt;page || i&lt;=amount}&quot;&gt;<br>       {{i + 1}} <br>       &lt;img src=&quot;{{item?.player?.officialImageSrc}}&quot; alt=&quot;basketball player&quot;&gt;<br>       {{ item.player.firstName + &#39; &#39; + item.player.lastName}} - {{item?.player?.primaryPosition}} | #{{item.player.jerseyNumber}} {{item?.stats?.offense?.pts}} Pts<br>     &lt;/span&gt;<br>   &lt;/div&gt;<br> &lt;/div&gt;</pre><pre>//app.component.scss<br>.card {<br>  background: #444;<br>  width: 33%;<br>  margin: 20px;<br>  padding: 20px;<br>  box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.2), 0px 6px 10px 0px rgba(0, 0, 0, 0.14), 0px 1px 18px 0px rgba(0, 0, 0, 0.12);<br>   .dn {<br>     display: none;<br>   }<br>  .content {<br>    font-size: 16px;<br>    img {<br>      height: 45px;<br>      width: 45px;<br>      box-shadow: 0 3px 1px -2px rgba(0, 0, 0, 0.2), 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12);<br>      border: 2px solid #fff;<br>      border-radius: 50%;<br>      object-fit: cover;<br>      margin-right: 3px;<br>    }<br>  }<br>}</pre><p>This will give you a nice ui friendly list with some basic pagination to increase the user experience. This will lay the groundwork for listing and sorting other stats as well. Enjoy :)</p><p>Part 1: <a href="https://www.ianposton.com/angular-9-and-mysportsfeed-api-part-1/">Getting Started with Angular 9 and MSF</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b317b0b54b83" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular 9 and MySportsFeeds API Part 1]]></title>
            <link>https://ianposton.medium.com/angular-9-and-mysportsfeeds-api-part-1-34663724e4e2?source=rss-f54ad2f332cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/34663724e4e2</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[sports]]></category>
            <category><![CDATA[fantasy-sports]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Ian Poston]]></dc:creator>
            <pubDate>Thu, 02 Apr 2020 22:56:16 GMT</pubDate>
            <atom:updated>2021-07-06T23:23:40.090Z</atom:updated>
            <content:encoded><![CDATA[<h3>Getting Started with Angular 9 and MySportsFeeds</h3><p>This is for sports stats lovers trying to build something with the latest angular framework. This article will show you how to get started using Angular 9 with the MySportsFeeds API.</p><p>I will fetch a list of NBA players, filter the response and sort the NBA players with the highest point total.</p><p>You will learn</p><ul><li>Make an api request with the HttpClient Module</li><li>Filter the api response</li><li>Display the data</li></ul><p>First you will need to be given access MySportsFeeds endpoints. As a developer working on a non-commercial app for a reduced monthly fee you can be given access to their endpoints. Let MSF know that you are working on a non-commercial project and they will send you an api token. Sign up at <a href="https://www.mysportsfeeds.com/">MySportsFeeds</a> and use the api token in the header request to authenticate the api get request. let headers = new HttpHeaders().set(&quot;Authorization&quot;, &quot;Basic &quot; + btoa(apiToken + &quot;:&quot; + &#39;MYSPORTSFEEDS&#39;));</p><p>The first thing I want to do in this app is get a list of all the active NBA players and their current season stats. I used the <a href="https://www.mysportsfeeds.com/data-feeds/api-docs/#">MySportsFeeds API Documentation</a> to find the correct endpoint to get NBA player stats for 2019-2020. I am able to use Angular’s <a href="https://angular.io/guide/http">HttpClient</a> module to send a GET request for data using this endpoint https://api.mysportsfeeds.com/v2.1/pull/nba/2019-2020-regular/player_stats_totals.json?position=PG,SG,SF,PF,C found in the api’s documentation.</p><p>This api request will send 718 items which is a rather large payload especially for the ui. I will filter the response with javascript so that I will only have relative NBA players before I define the array for the ui. I can use the gamesPlayed data attribute to remove irrelevant players from the array.</p><pre>//app.component.ts <br><br>import { Component, OnInit  } from &#39;@angular/core&#39;;<br>import { HttpClient, HttpResponse, HttpHeaders, HttpRequest} from &#39;@angular/common/http&#39;;<br><br>let headers = new HttpHeaders().set(&quot;Authorization&quot;, &quot;Basic &quot; + btoa(apiToken + &quot;:&quot; + &#39;MYSPORTSFEEDS&#39;));<br>let url = &#39;https://api.mysportsfeeds.com/v2.1/pull/nba/2019-2020-regular/player_stats_totals.json?position=PG,SG,SF,PF,C&#39;;<br><br>@Component({<br>  selector: &#39;app-root&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.scss&#39;]<br>})<br><br>export class AppComponent implements OnInit {<br>   <br>   public nbaData: Array&lt;any&gt;;<br><br>   constructor(private http: Http) {}<br><br>   loadData() {<br>    this.http.get(url, {headers})<br>      .subscribe(res =&gt; {<br>        console.log(res[&#39;playerStatsTotals&#39;], &#39;NBA players and stats&#39;);<br>        this.nbaData = res[&#39;playerStatsTotals&#39;].filter(<br>          player =&gt; player.stats != null &amp;&amp; player.stats.gamesPlayed &gt; 5);<br>      });<br>   }<br><br>   ngOnInit() {<br>    loadData();<br>   }<br>}</pre><pre>//app.component.html<br><br> &lt;div class=&quot;card&quot; *ngIf=&quot;nbaData != null&quot;&gt;<br>   &lt;h2&gt;Points&lt;/h2&gt;<br>   &lt;div *ngFor=&quot;let item of nbaData&quot;&gt;<br>     &lt;img src=&quot;{{item?.player?.officialImageSrc}}&quot; alt=&quot;basketball player&quot;&gt;<br>     {{ item.player.firstName + &#39; &#39; + item.player.lastName}} - {{item?.player?.primaryPosition}} | #{{item.player.jerseyNumber}} {{item?.stats?.offense?.pts}} Pts<br>   &lt;/div&gt;<br> &lt;/div&gt;</pre><pre>//app.component.scss<br>.card {<br>  width: 33%;<br>  margin: 20px;<br>  padding: 20px;<br>  box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.2), 0px 6px 10px 0px rgba(0, 0, 0, 0.14), 0px 1px 18px 0px rgba(0, 0, 0, 0.12);<br>}</pre><p>This will produce a very long list of players and their point total on the season. I am showing my list of players inside a simple card. I’ve added <a href="https://ianposton.medium.com/angular-9-and-mysportsfeeds-api-part-2-b317b0b54b83">Part 2</a> of this tutorial and I will sort the list from most points to least and I will set up a simple pagination to make the list easier to read.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34663724e4e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular 9, Heroku and Node.js: Encrypt and Decrypt data with Crypto-js]]></title>
            <link>https://ianposton.medium.com/angular-9-heroku-and-node-js-encrypt-and-decrypt-data-with-crypto-js-528127f6843e?source=rss-f54ad2f332cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/528127f6843e</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[encryption]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[decryption]]></category>
            <dc:creator><![CDATA[Ian Poston]]></dc:creator>
            <pubDate>Thu, 05 Mar 2020 07:36:33 GMT</pubDate>
            <atom:updated>2020-11-30T05:38:34.593Z</atom:updated>
            <content:encoded><![CDATA[<h3>Encrypt / Decrypt apiKey with Crypto-js</h3><p>This is primarily for an existing angular app but this may be applicable for other use cases regarding encrypting data via node.js / express server.</p><p>If you have a scenario in where you might need to send a private apiKey to the client side of an Angular app this is an example of how it can be done using <a href="https://github.com/brix/crypto-js">Crypto-js</a>. In this example I am fetching a private config variable from a heroku app and sending the apiKey to the client-side. Since I don’t want the the apiKey exposed in the dev tools Network I must first encrypt the apiKey. For Heroku this app uses a node.js / express server file app.js.</p><ul><li>Create a new file in the root of the app touch app.js</li><li>To set up a node.js express server run npm i express http path --save</li></ul><pre>//app.js </pre><pre>const express = require(&#39;express&#39;);<br>const http = require(&#39;http&#39;);<br>const path = require(&#39;path&#39;);<br>const app = express();</pre><pre>app.use(express.static(path.join(__dirname, &#39;dist&#39;)));</pre><pre>//SPECIFY NG-BUILD PATH<br>app.get(&#39;*&#39;, (req, res) =&gt; {<br>  res.sendFile(path.join(__dirname, &#39;dist/index.html&#39;))<br>});</pre><pre>//HEROKU PORT<br>const port = process.env.PORT || &#39;3001&#39;;<br>app.set(&#39;port&#39;, port);</pre><pre>const server = http.createServer(app);<br>server.listen(port, () =&gt; console.log(`Running on localhost:${port}`));</pre><ul><li>Run ng build to build the app in the dist directory. dist/index.html</li><li>Run node app.js to serve the app at <a href="http://localhost:3001.">http://localhost:3001.</a></li><li>The Procfile in this app’s root specifies the server for heroku to use.</li></ul><pre>//Procfile</pre><pre>web: node app.js</pre><ul><li>This &quot;main&quot;: &quot;app.js&quot; line in package.json specifies how to tell heroku to look for app.js.</li><li>Before pushing to github, before heroku deploy set Config Variables.</li></ul><p>Adding the environment variable apiKey for the api. I didn’t want to share my apiKey headers information in my github repository so I added my apiKey to my Config Variables for heroku to use. I stored the apiKey in my heroku app by going to the app settings in my heroku dashboard. Click on Config Variables and add the key (name) and value (apiKey) there. It will be secured privately away from view. You can call it to the client side by adding this code to the app.js file. I called my env TOKEN and made the value the apiKey.</p><p>Before I send my TOKEN to the client-side I want to encrypt it by using the Crypto-js module.</p><ul><li>To encrypt the apiKey (TOKEN) before sending to the client-side run npm i crypto-js --save</li></ul><pre>//app.js </pre><pre>const express = require(&#39;express&#39;);<br>const http = require(&#39;http&#39;);<br>const path = require(&#39;path&#39;);<br>const CryptoJS = require(&quot;crypto-js&quot;);<br>const app = express();</pre><pre>let TOKEN = &#39;&#39;;<br>let ciphertext = null;</pre><pre>app.use(express.static(path.join(__dirname, &#39;dist&#39;)));</pre><pre>//SEND API KEY TO FRONT-END APP.COMPONENT.TS<br>app.get(&#39;/heroku-env&#39;, function(req, res){<br>  ciphertext = CryptoJS.AES.encrypt(process.env.TOKEN, &#39;myPassword&#39;).toString();<br>  TOKEN = ciphertext;<br>  res.json(TOKEN);<br>});</pre><pre>//SPECIFY NG-BUILD PATH<br>app.get(&#39;*&#39;, (req, res) =&gt; {<br>  res.sendFile(path.join(__dirname, &#39;dist/index.html&#39;))<br>});</pre><pre>//HEROKU PORT<br>const port = process.env.PORT || &#39;3001&#39;;<br>app.set(&#39;port&#39;, port);</pre><pre>const server = http.createServer(app);<br>server.listen(port, () =&gt; console.log(`Running on localhost:${port}`));</pre><ul><li>Encrypt the apiKey (TOKEN) with Crypto-js using a made up password as the right param.</li><li>app.get &#39;/heroku-env&#39; will send the encrypted token when called from the client-side.</li><li>Get the apiKey sent from the server to the client-side using Http. this.http.get(&#39;/heroku-env&#39;)</li><li>Decrypt the apiKey (TOKEN) when fetched from server by importing Crypto-js using the same password param.</li></ul><p>The apiKey will be sent as an encrypted string. The res (response) from app.js will then need to be decrypted before adding to httpHeaders to allow authentication of an api request. Each api request may be a little different but this example should get you close to how you would make a proper api request.</p><pre>//app.component.ts </pre><pre>import { Component, Inject, OnInit } from &#39;@angular/core&#39;;<br>import { HttpClient, HttpHeaders } from &#39;@angular/common/http&#39;;<br>import * as CryptoJS from &#39;crypto-js&#39;;</pre><pre>@Component({<br>  selector: &#39;app-root&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.scss&#39;]<br>})</pre><pre>export class AppComponent implements OnInit {</pre><pre>   constructor(private http: Http) {}</pre><pre>   getEnv() {<br>      console.log(&quot;trying to get heroku env...&quot;);<br>      this.http.get(&#39;/heroku-env&#39;)<br>      .subscribe(res =&gt; {<br>        let bytes  = CryptoJS.AES.decrypt(res, &#39;myPassword&#39;);<br>        let key = bytes.toString(CryptoJS.enc.Utf8);<br>        headers = new HttpHeaders().set(&quot;Authorization&quot;, &quot;Basic &quot; + btoa(key + &quot;:&quot; + &#39;MYAPIFEED&#39;));<br>      });<br>   }</pre><pre>   ngOnInit() {<br>    this.getEnv();<br>   }<br>}</pre><h4>Deploy to Heroku</h4><p>If you are trying to deploy to heroku for the first time and your private keys are hidden proceed with the following steps. After you git push to your github repo follow the steps below. Assuming you have a heroku account and installed the heroku toolbelt.</p><ol><li>run heroku log in</li><li>run heroku create name-of-app</li><li>run git push heroku master</li><li>If deploy is successful run heroku open</li></ol><p>If there were problems during deploy and you are trying this from scratch here are some requirements heroku needs to deploy.</p><ol><li>Have @angular/cli and @angular/compiler-cli listed under dependencies in package.json.</li><li>Add &quot;postinstall&quot;: &quot;ng build&quot; to the package.json’s &quot;scripts&quot; object.</li></ol><pre>//package.json</pre><pre>&quot;main&quot;: &quot;app.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;ng&quot;: &quot;ng&quot;,<br>    &quot;start&quot;: &quot;ng serve&quot;,<br>    &quot;build&quot;: &quot;ng build&quot;,<br>    &quot;test&quot;: &quot;ng test&quot;,<br>    &quot;lint&quot;: &quot;ng lint&quot;,<br>    &quot;e2e&quot;: &quot;ng e2e&quot;,<br>    &quot;postinstall&quot;: &quot;ng build&quot;<br>  },<br>  &quot;engines&quot;: {<br>    &quot;node&quot;: &quot;~10.16.2&quot;,<br>    &quot;npm&quot;: &quot;~6.13.7&quot;<br>  },<br>  &quot;dependencies&quot;: {<br>    ...</pre><pre>    &quot;@angular/cli&quot;: &quot;9.0.1&quot;,<br>    &quot;@angular/compiler-cli&quot;: &quot;^9.0.0&quot;,</pre><pre>  ...</pre><p>To test that the encryption is working after deploying to heroku open your app and check the network tab in chrome dev tools. Search for the request heroku-env and click on the response tab. You should see an encrypted string.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=528127f6843e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create React App Deploy to Heroku]]></title>
            <link>https://ianposton.medium.com/create-react-app-deploy-to-heroku-7c3c03f34382?source=rss-f54ad2f332cd------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c3c03f34382</guid>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ian Poston]]></dc:creator>
            <pubDate>Wed, 28 Mar 2018 20:57:38 GMT</pubDate>
            <atom:updated>2018-09-28T17:38:02.056Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deploy create react app to Heroku. <a href="https://create-react-test.herokuapp.com/">Demo</a></h3><p>NPM (Node Package Manager), Node.js, Git, GitHub, Heroku and knowledge of how to write commands in a terminal will be required for this project. There are many ways to install NPM, Git and Node.js on your machine. I recommend researching the best way to install this software. Visiting the <a href="https://www.npmjs.com/">NPM</a>, <a href="https://git-scm.com/">Git</a> and the <a href="https://nodejs.org/en/">Node.js</a> website is a good place to start.</p><p>I used a mac to create this app and I use <a href="https://www.iterm2.com/">iterm2</a> for my terminal. This is not required, every machine will come with its own terminal.</p><h3>Software used for this application</h3><ul><li>React (version 16.2.0)</li><li><a href="https://github.com/facebook/create-react-app">Create React App</a></li><li>Heroku <a href="https://www.heroku.com/">Set up a free account</a></li><li>Github <a href="https://github.com/">Set up a free account</a></li><li>NPM (version 5.6.0)</li><li>Node (version 6.10.3)</li><li><a href="https://expressjs.com/">Express</a> (version 4.16.3)</li><li>git (version 2.10.1)</li><li>iterm2 (version 3.1.5)</li></ul><p>I like to start projects by creating a directory on my desktop by typing commands in the terminal.</p><ul><li>Open the terminal and type cd desktop. This command takes you to your desktop.</li><li>Make a folder call react. In the terminal type mkdir react and go into the folder cd react</li><li>Create React App. In the terminal type npx create-react-app app-name (npx comes with npm 5.2+ and higher) and go into the app cd app-name</li><li>Type npm run-script build to build the app in a directory named build. The build directory will appear in the root of the app.</li><li>Create React App comes with a live reload server. Type npm start to serve the app with live reload. Heroku uses the Node.js server when this app is deployed, typing npm run-script build is the way heroku can connect the server with our app.</li><li>Create an app.js file in the root of the app touch app.js.</li><li>Type npm install express --save. This command will install express in the node_modules folder and add it to the package.json file. (I am noticing that installing express removed some files.<br>I fixed this by typing npm install to install all the necessary files for this app)</li><li>For Heroku this app points to the app.js server which uses Node.js and Express. (Node.js is required to serve this app locally)</li></ul><pre>//app.js</pre><pre>const express = require(&#39;express&#39;);<br>const http = require(&#39;http&#39;);<br>const path = require(&#39;path&#39;);</pre><pre>let app = express();</pre><pre>app.use(express.static(path.join(__dirname, &#39;build&#39;)));</pre><pre>const port = process.env.PORT || &#39;8080&#39;;<br>app.set(&#39;port&#39;, port);</pre><pre>const server = http.createServer(app);<br>server.listen(port, () =&gt; console.log(`Running on localhost:${port}`));</pre><ul><li>Type node app.js to serve the app at <a href="http://localhost:8080.">http://localhost:8080.</a></li><li>Create a file called Procfile in the root of the app. Type touch Procfile. This specifies the server for heroku to use.</li></ul><pre>//Procfile</pre><pre>web: node app.js</pre><ul><li>A few adjustments to package.json are necessary for the heroku deploy process. postinstall and engines are important parts of package.json for a successful deploy.</li></ul><pre>//package.json</pre><pre>{<br>  &quot;name&quot;: &quot;app-name&quot;,<br>  &quot;version&quot;: &quot;0.1.0&quot;,<br>  &quot;private&quot;: true,<br>  &quot;main&quot;: &quot;app.js&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;start&quot;: &quot;react-scripts start&quot;,<br>    &quot;build&quot;: &quot;react-scripts build&quot;,<br>    &quot;test&quot;: &quot;react-scripts test --env=jsdom&quot;,<br>    &quot;eject&quot;: &quot;react-scripts eject&quot;,<br>    &quot;postinstall&quot;: &quot;react-scripts build&quot;<br>  },<br>  &quot;engines&quot;: {<br>    &quot;node&quot;: &quot;~6.10.3&quot;,<br>    &quot;npm&quot;: &quot;~5.6.0&quot;<br>  },<br>  &quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.16.3&quot;,<br>    &quot;react&quot;: &quot;^16.2.0&quot;,<br>    &quot;react-dom&quot;: &quot;^16.2.0&quot;,<br>    &quot;react-scripts&quot;: &quot;1.1.1&quot;<br>  }<br>}</pre><ul><li>Assuming the heroku account is created, the <a href="https://devcenter.heroku.com/articles/heroku-cli">heroku toolbelt</a> is installed, and a github account is setup with a new repository make an initial commit and push to the github repo, then login to heroku from the terminal, create a heroku app and push to heroku. Steps as follow.</li><li>Type git init then type git add -A then typegit commit -am &quot;initial commit&quot; then git remote add origin <a href="https://github.com/user-name/repo-name.git">https://github.com/user-name/repo-name.git</a>. (pushing the app to a new github repo. Create React App will automatically create a .gitignore file to avoid pushing node_modules and the build directory)</li><li>Login to heroku heroku login enter heroku password. (heroku toolbelt required)</li><li>Create heroku app heroku create app-name. (this will add the heroku remote)</li><li>Deploy to heroku git push heroku master.</li></ul><p>There may be an error during deploy pertaining to package.lock.json and yarn.lock. Heroku will allow only one lock file. Keep package.lock.json Delete yarn.lock from the app, then type git commit -am &quot;delete yarn.lock then type git push and then git push heroku master and the error will go away.</p><p>Each time there is a major change to the app commit and push to github then git push heroku master to keep the two master branches in sync.</p><p>Creact React App live on Heroku: <a href="https://create-react-test.herokuapp.com/">https://create-react-test.herokuapp.com/</a></p><p>The working code on github: <a href="https://github.com/iposton/create-react-heroku">https://github.com/iposton/create-react-heroku</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c3c03f34382" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>