<?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: John</title>
    <description>The latest articles on DEV Community by John (@johnhenry).</description>
    <link>https://dev.to/johnhenry</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%2F169286%2F1a989418-93f0-4f9b-87bd-b6d6b2dc163a.png</url>
      <title>DEV Community: John</title>
      <link>https://dev.to/johnhenry</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johnhenry"/>
    <language>en</language>
    <item>
      <title>Visual Studio Code on a remote server</title>
      <dc:creator>John</dc:creator>
      <pubDate>Sun, 08 Sep 2019 02:14:35 +0000</pubDate>
      <link>https://dev.to/johnhenry/visual-studio-code-on-a-remote-server-2dkh</link>
      <guid>https://dev.to/johnhenry/visual-studio-code-on-a-remote-server-2dkh</guid>
      <description>&lt;p&gt;These days, it is easy to set up a web server where you can program remotely using virtually any client device.&lt;/p&gt;

&lt;p&gt;It is not difficult to do this using secure shell alone, but here are a few methods that can enhance this experience using the &lt;a href="https://code.visualstudio.com"&gt;Visual Studio Code&lt;/a&gt; editor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Definitions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;CL = Command Line&lt;/li&gt;
&lt;li&gt;FS = File System&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pre-Requisites
&lt;/h2&gt;

&lt;p&gt;This tutorial assumes that you are already famaliar with using &lt;a href="https://en.wikipedia.org/wiki/Secure_Shell"&gt;secure shell (SSH)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You &lt;em&gt;should&lt;/em&gt; at least be famaliar with logging into a remote server using &lt;a href="https://www.ssh.com/ssh/key/"&gt;key-based authentication&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 1 : code-server
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/cdr/code-server"&gt;Code-server&lt;/a&gt; creates a web server with an instance of a web version of Visual Studio Code that you can access via a browser.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install code-server on your server.&lt;/li&gt;
&lt;li&gt;Access server's CL via SSH.&lt;/li&gt;
&lt;li&gt;On server, run  &lt;code&gt;code-server &amp;lt;path to workspace or directory&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You can access server's FS and CL through password-secured web interface at :8443&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Method 2 : sshcode
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/cdr/sshcode"&gt;Sshcode&lt;/a&gt; works similarly to code-server (it's actually built on top of it) but uses a client program to set up the web server. Additionally, if you're using chrome, it will open a customized window with Visual Studio Code keybinding in tact.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install chrome and sshcode on client&lt;/li&gt;
&lt;li&gt;On client, run &lt;code&gt;sshcode &amp;lt;user&amp;gt;@&amp;lt;serveraddress&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Access server's FS and CL through chrome window.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Method 3 : Visual Studio Code
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Install &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt; on client&lt;/li&gt;
&lt;li&gt;Install &lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh"&gt;Remote - SSH&lt;/a&gt; extension&lt;/li&gt;
&lt;li&gt;From extenison menu, connect @&lt;/li&gt;
&lt;li&gt;Access server's FS and CL through Visual Studio Code&lt;/li&gt;
&lt;li&gt;From extension menu, tunnel ports from server to client.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;To supplement the above methods, you may want to learn about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;mounting file remote folders locally and using any editor you want using &lt;a href="https://github.com/libfuse/sshfs"&gt;sshfs&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;accessing other resources on your remote maching via &lt;a href="https://www.ssh.com/ssh/tunneling/example"&gt;tunneling&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;mitigating unstable remote [mobile] connections with &lt;a href="https://mosh.org/"&gt;mosh&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;customizations and pitfalls of above programs by reading their documetion.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>remote</category>
      <category>code</category>
      <category>vscode</category>
      <category>ssh</category>
    </item>
    <item>
      <title>Angular Done Right: Part 1 — Binding and Directives
</title>
      <dc:creator>John</dc:creator>
      <pubDate>Sun, 08 Sep 2019 02:13:52 +0000</pubDate>
      <link>https://dev.to/johnhenry/angular-done-right-part-1-binding-and-directives-21c7</link>
      <guid>https://dev.to/johnhenry/angular-done-right-part-1-binding-and-directives-21c7</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally Published: &lt;a href="https://medium.com/@iamjohnhenry/angular-done-right-part-1-binding-and-directives-6112a43aa1dc"&gt;https://medium.com/@iamjohnhenry/angular-done-right-part-1-binding-and-directives-6112a43aa1dc&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Author’s Note: While I no longer program heavily using angular, in favor of using smaller, more modular libraries, the reader will find the ideas presented in this series useful.&lt;/p&gt;

&lt;p&gt;Angular is hard, right?&lt;/p&gt;

&lt;p&gt;Well, one thing that’s for sure is that Angular is a massive. It’s a full application framework made of components including directives, controllers, services, and much much more. It’s shrouded in strange new concepts such as “&lt;em&gt;Dependency Injection&lt;/em&gt;” and “&lt;em&gt;Transclausion&lt;/em&gt;”. Despite it’s overall allure, anyone creating an angular app for the first time must accept that he or she is in for a steep learning curve due before being able to build anything fun or useful.&lt;/p&gt;

&lt;p&gt;That’s the approach that most tutorials on the web take. In this series we take different approach. We’ll gradually build upon the most basic concepts in angular; first introducing the bare minimum needed to build an application and building upon it as we go along.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Angular
&lt;/h3&gt;

&lt;p&gt;We’ll start with a very basic HTML page. Create a basic html page with the following code inside:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The next thing that you need to do is to download and include the angular.js file. You can download it directly from the &lt;a href="https://angularjs.org/"&gt;Angular JS homepage&lt;/a&gt;, or, if you perfer, using the &lt;a href="http://bower.io/"&gt;Bower package manager&lt;/a&gt;. I won’t go into specifics, on how exactly how to do that, but having made it this far, I doubt that you’ll have any trouble :).&lt;/p&gt;

&lt;p&gt;Once you’ve included the angular file in your app, your file should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;”path/to/angular.js”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Bootstrapping
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Bootstraping&lt;/strong&gt; is a term used for connecting your angular application to the web page. Don’t get it confused with the popular &lt;a href="http://getbootstrap.com/"&gt;UI framework&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are two ways of bootstrapping an application. The easiest and most reliable way is to add the *&lt;em&gt;ng-app *&lt;/em&gt;&lt;em&gt;directive&lt;/em&gt; to an html element. That element and all of it’s children will be part of your angular app.&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;What are directives?&lt;/em&gt;”, you must be wondering… Well, for now, just think of them as attibues that you can add to your html that let your app interact with angular. We’ll learn more about directives very soon, and in a later part of the series.&lt;/p&gt;

&lt;p&gt;We could put it on any element — the &lt;strong&gt;&lt;/strong&gt; tag; a &lt;strong&gt; tag — but lets go ahead and put it on the top level “html” element. This way, the entire page will be available to our application.&lt;br&gt;

&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;ng-app&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;”path/to/angular.js”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;There is another way to bootstrap your application (it actually uses the &lt;a href="https://docs.angularjs.org/api/ng/function/angular.bootstrap"&gt;angular.bootstrap&lt;/a&gt; method!), but I have found it to be much more error-prone, so I wont at least into that method… at least not today.&lt;/p&gt;
&lt;h3&gt;
  
  
  Binding
&lt;/h3&gt;

&lt;p&gt;— ng-model and ng-bind&lt;/p&gt;

&lt;p&gt;Now that you’ve set up the application, binding data is the simplest thing that you can do in angular. Try modifying your html to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;ng-app&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;body&lt;/span&gt; &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;ng-model=&lt;/span&gt;&lt;span class="s"&gt;”input”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;ng-bind=&lt;/span&gt;&lt;span class="s"&gt;”input”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;”path/to/angular.js”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;You’ve added two new directives. The** ng-model **&lt;em&gt;directives&lt;/em&gt; allows you to bind a variable to a standard html element that receives user input. This can be either an input element, a textarea element, or select element. (If you’re familiar with jQuery, this might remind you of the &lt;a href="http://api.jquery.com/val/"&gt;val&lt;/a&gt; method.)&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;ng-bind&lt;/strong&gt; directive does something similar, but but it works by replacing the inner html of any html element with the bound variable. (If you’re familiar with jQuery, this might remind you of the &lt;a href="http://api.jquery.com/html/"&gt;html&lt;/a&gt; method.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;input&lt;/strong&gt; is the name that we have chosen for the name of the variable to be bound, but we could have chosen pretty much anything.&lt;/p&gt;

&lt;p&gt;Note: the variables exist within the scope of the application and not on the page. A variable defined within javascript will not be bound. We’ll learn exactly “where” these variables live in a later chapter on controllers and scope.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
     &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;Sorry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;I&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;available&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;binding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;You can also use the double curly braces syntax, &lt;code&gt;“{{input}}”&lt;/code&gt;, instead of &lt;strong&gt;ng-bind&lt;/strong&gt; to add binding anywhere without the need for an HTML element:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="cp"&gt;&amp;lt;!doctype html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;ng-app&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;body&lt;/span&gt; &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;ng-model=&lt;/span&gt;&lt;span class="s"&gt;”input”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;span&amp;gt;&lt;/span&gt;{{input}}&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;”path/to/angular.js”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;If you open the page in a browser, you will see an empty box, and when you type box, you’ll see that it’s bound to the span right next to it.&lt;/p&gt;

&lt;p&gt;This might be useful for, say, a form:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
     Name:&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;ng-model=&lt;/span&gt;&lt;span class="s"&gt;”user.name”&lt;/span&gt; &lt;span class="na"&gt;ng-required=&lt;/span&gt;&lt;span class="s"&gt;”true”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
     Favorite Color:
     &lt;span class="nt"&gt;&amp;lt;select&lt;/span&gt; &lt;span class="na"&gt;ng-model=&lt;/span&gt;&lt;span class="s"&gt;”user.color”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;”not&lt;/span&gt; &lt;span class="na"&gt;available&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;N/A&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;”red”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Red&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;”green”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Green&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;option&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;”blue”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Blue&lt;span class="nt"&gt;&amp;lt;/option&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;/select&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;”background-color:{{user.color}}”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Submit&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Your name is
     &lt;span class="nt"&gt;&amp;lt;b&lt;/span&gt; &lt;span class="na"&gt;ng-bind=&lt;/span&gt;&lt;span class="s"&gt;”user.name&lt;/span&gt; &lt;span class="err"&gt;||&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="na"&gt;Blank&lt;/span&gt;&lt;span class="err"&gt;’”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/b&amp;gt;&lt;/span&gt;
      and your favorite color is
     &lt;span class="nt"&gt;&amp;lt;b&lt;/span&gt; &lt;span class="na"&gt;ng-bind=&lt;/span&gt;&lt;span class="s"&gt;”user.color”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/b&amp;gt;&lt;/span&gt;.
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;Notice that the &lt;strong&gt;user.name&lt;/strong&gt; and the &lt;strong&gt;user.color&lt;/strong&gt; properties are bound to other parts of the form.&lt;/p&gt;

&lt;p&gt;You may have spotted the &lt;strong&gt;ng-required&lt;/strong&gt; directive on the input tag. That’s prevents a form from being submitted unless the required value is filled out. Try submitting the form below without typing in a name. (Be Warned: This may not work in all browsers.)&lt;/p&gt;

&lt;p&gt;It’s also useful to be able to perform calculations on the page. Here’s a simple tip calculator. It a bit off (check out those rounding errors!), but it starts to show you what you can do with an angular app.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight html"&gt;&lt;code&gt;    Bill: &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;ng-model=&lt;/span&gt;&lt;span class="s"&gt;”bill”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;br/&amp;gt;&lt;/span&gt;
    Tip Percentage: &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;”number”&lt;/span&gt; &lt;span class="na"&gt;ng-model=&lt;/span&gt;&lt;span class="s"&gt;”tip”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; %
    &lt;span class="nt"&gt;&amp;lt;br/&amp;gt;&lt;/span&gt;
    Total: $&lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;ng-bind=&lt;/span&gt;&lt;span class="s"&gt;”(1&lt;/span&gt; &lt;span class="err"&gt;+&lt;/span&gt; &lt;span class="err"&gt;(&lt;/span&gt;&lt;span class="na"&gt;tip&lt;/span&gt;&lt;span class="err"&gt;/100)&lt;/span&gt; &lt;span class="err"&gt;)&lt;/span&gt; &lt;span class="na"&gt;*&lt;/span&gt; &lt;span class="na"&gt;bill&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;h3&gt;
  
  
  A Few More Directives
&lt;/h3&gt;

&lt;p&gt;— Showing and Hiding Elements with ng-if, ng-show, ng-hide&lt;/p&gt;

&lt;p&gt;Conveniently, attaching the &lt;strong&gt;ng-model&lt;/strong&gt; to a checkbox or radio allows you to bind to its &lt;strong&gt;checked&lt;/strong&gt; property rather than it’s value.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;”checkbox”&lt;/span&gt; &lt;span class="na"&gt;ng-init=&lt;/span&gt;&lt;span class="s"&gt;”checked=true”&lt;/span&gt; &lt;span class="na"&gt;ng-model=&lt;/span&gt;&lt;span class="s"&gt;”checked”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;ng-show=&lt;/span&gt;&lt;span class="s"&gt;”checked”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;The box is checked&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;ng-hide=&lt;/span&gt;&lt;span class="s"&gt;”checked”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;The box is not checked&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;ng-if=&lt;/span&gt;&lt;span class="s"&gt;”checked”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; :) &lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;And we can use this fact to demonstrate a few new directives.&lt;/p&gt;

&lt;p&gt;Although I’m not using for it’s single recommended &lt;a href="https://docs.angularjs.org/api/ng/directive/ngInit"&gt;use case&lt;/a&gt;, the &lt;strong&gt;ng-init&lt;/strong&gt;&lt;br&gt;
directive allows you to initialize a variable’s value — otherwise it starts off as empty.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;ng-if&lt;/strong&gt;, &lt;strong&gt;ng-show&lt;/strong&gt;, and &lt;strong&gt;ng-hide&lt;/strong&gt; directives work to show and hide an element. The difference is that &lt;strong&gt;ng-if&lt;/strong&gt; will remove an element from the page, if not visible, while &lt;strong&gt;ng-show&lt;/strong&gt; and &lt;strong&gt;ng-hide&lt;/strong&gt; will simply hide it.&lt;/p&gt;

&lt;p&gt;— Repeating Elements with ng-repeat&lt;/p&gt;

&lt;p&gt;A final useful directive is the &lt;strong&gt;ng-repeat&lt;/strong&gt; directive that allows one to iterate through collections.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;ng-init=&lt;/span&gt;&lt;span class="s"&gt;”beatles&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;[&lt;/span&gt;
     &lt;span class="err"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="na"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="na"&gt;John&lt;/span&gt;&lt;span class="err"&gt;’},&lt;/span&gt;
     &lt;span class="err"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="na"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="na"&gt;Paul&lt;/span&gt;&lt;span class="err"&gt;’},&lt;/span&gt;
     &lt;span class="err"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="na"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="na"&gt;George&lt;/span&gt;&lt;span class="err"&gt;’},&lt;/span&gt;
     &lt;span class="err"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="na"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="na"&gt;Ringo&lt;/span&gt;&lt;span class="err"&gt;’}&lt;/span&gt;
     &lt;span class="err"&gt;]”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;ng-repeat=&lt;/span&gt;&lt;span class="s"&gt;”beatle&lt;/span&gt; &lt;span class="na"&gt;in&lt;/span&gt; &lt;span class="na"&gt;beatles&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;ng-bind=&lt;/span&gt;&lt;span class="s"&gt;”$index”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;.&lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;ng-bind=&lt;/span&gt;&lt;span class="s"&gt;”beatle.name”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;You’ll notice that in addition to the &lt;strong&gt;&lt;em&gt;beatle&lt;/em&gt;&lt;/strong&gt; property, the &lt;strong&gt;ng-repeat *&lt;em&gt;directive also provides a *&lt;/em&gt;*$index&lt;/strong&gt;* property that can also be bound to the application. This **&lt;em&gt;$index&lt;/em&gt; **is equal to the index of the iteration.&lt;/p&gt;

&lt;p&gt;— Modifying data with filters&lt;/p&gt;

&lt;p&gt;Just for the heck of it, let’s take a sneak preview of filters.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;ng-repeat=&lt;/span&gt;&lt;span class="s"&gt;”beatle&lt;/span&gt; &lt;span class="na"&gt;in&lt;/span&gt; &lt;span class="na"&gt;beatles&lt;/span&gt; &lt;span class="err"&gt;|&lt;/span&gt; &lt;span class="na"&gt;orderBy:&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="err"&gt;’”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;ng-bind=&lt;/span&gt;&lt;span class="s"&gt;”$index”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;.&lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;ng-bind=&lt;/span&gt;&lt;span class="s"&gt;”beatle.name”&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;&lt;strong&gt;orderBy&lt;/strong&gt; may look similar to a directive, but it’s not, it’s a filter. Filters work within certain directives to modify the data within them. This particular one changes the order of items within an array. You can read more about this and other available filters &lt;a href="https://docs.angularjs.org/api/ng/filter"&gt;here&lt;/a&gt;. Like directives, filters are one of the nine or so components that make up an angular app and can be woven together with the others to make powerful applications.&lt;/p&gt;

&lt;p&gt;Filters, like Directives, are one of the core components of angular. And we’ll learn a lot more about them in an upcoming entry.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;With just few basic built built in components, you can start creating interesting apps with barely any code. I’ll be back soon with another update to this series, but in the meantime, start using Angular to make something cool!&lt;/p&gt;

&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>binding</category>
      <category>directives</category>
    </item>
    <item>
      <title>Browsers, Servers, and APIs</title>
      <dc:creator>John</dc:creator>
      <pubDate>Tue, 28 May 2019 03:13:36 +0000</pubDate>
      <link>https://dev.to/johnhenry/browsers-servers-and-apis-5d6a</link>
      <guid>https://dev.to/johnhenry/browsers-servers-and-apis-5d6a</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally Published: &lt;a href="https://medium.com/@iamjohnhenry/browsers-servers-and-apis-2f7b10523f39"&gt;https://medium.com/@iamjohnhenry/browsers-servers-and-apis-2f7b10523f39&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;(Or “The path to Isomorphic Javascript”)&lt;/p&gt;

&lt;p&gt;Note: Examples in the article were tested and run using &lt;a href="https://nodejs.org/en/"&gt;node&lt;/a&gt; and &lt;a href="http://browserify.org/"&gt;browserify&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you’re a modern JavaScript enthusiast, you’re familiar with isomorphic code — the idea that you can write code that will run in both browser and node environments. Because JavaScript is a standardized language, you might think that this would be a trivial matter. The problem comes not in the language itself, but in the &lt;a href="https://en.wikipedia.org/wiki/Application_programming_interface"&gt;API&lt;/a&gt;s, which are specific to each environment and not standardized to play well with others.&lt;/p&gt;

&lt;p&gt;Some actions in one environment do not make sense in the other and so they lack specific APIs. The browser isn’t given access to the file system, so the &lt;a href="https://nodejs.org/api/fs.html"&gt;fs object&lt;/a&gt; that comes with node would not make sense here. Conversely, when I was younger, I would make copious use of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/alert"&gt;window.alert&lt;/a&gt; for debugging, but a popup window won’t show up when you’re looking at a command line.&lt;/p&gt;

&lt;p&gt;There are some actions, however, that can be done both within node and within the browser. One of the most interesting is creating servers.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Standardizing Servers&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Traditionally you wouldn’t set up a server within a browser, but considering what a server does — accepts requests and responds asynchronously, we can think of many use cases.&lt;/p&gt;

&lt;p&gt;For instance, a server within a service worker would be able to intercept and respond to requests within the browser, without ever having to make a call across a network.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//example -- service worker
const server = …;//define later

self.addEventListener(‘fetch’, function(event) {
 return event.respondWith(new Promise((resolve, reject)=&amp;gt;{
   const response = server.respondTo(event.request);
   response.on(“finish”, ()=&amp;gt;{
     resolve(req);
   });
 }));
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We’ll come back to this idea...&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Competing Standards&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;So, I want to put a server in the browser — what are my options? Not long ago, I learned about the new &lt;a href="https://hacks.mozilla.org/2016/09/flyweb-pure-web-cross-device-interaction/"&gt;FlyWeb&lt;/a&gt; standard that &lt;a href="https://blog.mozilla.org/"&gt;Mozilla&lt;/a&gt; is pushing for creating servers within the browser. I was pretty excited until i realized how different it was from anything that already existed in node.&lt;/p&gt;

&lt;p&gt;Every other server that I’ve seen follows a particular pattern…&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;HTTP&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Node already has a built in simple API for creating servers via &lt;a href="https://nodejs.org/api/http.html"&gt;http&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//example -- http
//create server and define action
const server = require(“http”)
 .createServer((request, response)=&amp;gt;{
  ...//process request 
  response.end();//end response
});

//start server
server.listen(/*listening address*/);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This might be considered a standard, but many modern server applications are built to handle complex workflows.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Express&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As the http module is quite simplistic, the &lt;a href="https://github.com/expressjs/express"&gt;express&lt;/a&gt; module is the de-facto standard for creating servers within node. It separates creating the server from assigning it actions. It also makes handling complex workflows easier. Express servers have a “use” method. It allows the user to chain together multiple functions to act upon a request before a response is sent. These functions are called “middleware”.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//example -- express

//create server
const server = require(“express”)();

//register middleware
server.use((request, response, next)=&amp;gt;{
  ...//process request
  next();//go to next middleware
});

//register middleware
server.use((request, response, next)=&amp;gt;{
  ...//process request
  if(request.endResponse){
    response.end();//end response
  }else{
    next();//go to next middleware
  }
});

//register middleware
server.use((request, response)=&amp;gt;{
  response.end();//end response
});

//start server
server.listen(/*listening address*/);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In addition, express adds additional features such as templating or routing which you may or may not need.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Koa 2&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/koajs/koa"&gt;Koa&lt;/a&gt; was created by the same people who create express and is considered to be its spiritual successor. It’s also my server of choice, so you should &lt;a href="http://koajs.com/"&gt;take a look at how it works&lt;/a&gt; if you haven’t already.&lt;/p&gt;

&lt;p&gt;Koa makes use of &lt;a href="https://github.com/tc39/ecmascript-asyncawait"&gt;asynchronous functions&lt;/a&gt; and adding middleware works similarly.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//example -- koa

const server = require(“koa”)();

server.use(async (context, next)=&amp;gt;{
  ...//process request
  next();//go to next middleware
});

server.use((context, next)=&amp;gt;{
  context.res.end();//end response
});

//start server
server.listen(/*listening address*/);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Koa also eschews built in routing and templating in favor of including them externally as middleware.&lt;/p&gt;

&lt;p&gt;Please be aware that Koa 2 is still in alpha, but since its API is so much cleaner than Koa 1’s, &lt;a href="https://www.smashingmagazine.com/2016/08/getting-started-koa-2-async-functions/"&gt;here’s a tutorial that will help you jump right in&lt;/a&gt;,&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Rill&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/rill-js/rill"&gt;Rill&lt;/a&gt; is a &lt;a href="https://medium.com/@pierceydylan/isomorphic-javascript-it-just-has-to-work-b9da5b0c8035#.l79bgqqwx"&gt;new server&lt;/a&gt; that has basically the same API as KOA 2, but works in the browser as well as in node right out of the box.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//example -- rill

const server = require(“rill”)();

server.use((context, next)=&amp;gt;{
  context.res.end();//end response
});

server.listen(/*listening address*/);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Rill may be overkill for my purposes, as it includes routing features that manipulate the web page, in addition to simply processing requests into responses.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;FlyWeb&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;There’s a basic pattern of that all of the other servers follow.&lt;/p&gt;

&lt;p&gt;1 create server&lt;/p&gt;

&lt;p&gt;2 add actions&lt;/p&gt;

&lt;p&gt;3 start server&lt;/p&gt;

&lt;p&gt;While rill or koa would have been a great starting point for a server in the browser, mozilla has taken a different route with their new FlyWeb servers.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//example -- flyweb

//create and start server
const server = await window.navigator.publishServer(/*listening address*/);

//add actions
server.onfetch = (request)=&amp;gt;{
  ...//process request
  request.end();//end response
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This breaks the pattern of everything we’ve seen so far! Here the server is created and started first. Only afterwards are actions actions added.&lt;/p&gt;

&lt;p&gt;Do we really need an entirely new way of doing basically the same thing?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aRfkqBjY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2A9nMBMt-OugnruBr_M-WuEQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aRfkqBjY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2A9nMBMt-OugnruBr_M-WuEQ.png" alt="I know. It’s in every article. Perhaps that says something to the importance of the situation. [https://xkcd.com/license.html](https://xkcd.com/license.html)" width="500" height="283"&gt;&lt;/a&gt;&lt;em&gt;I know. It’s in every article. Perhaps that says something to the importance of the situation. &lt;a href="https://xkcd.com/license.html"&gt;https://xkcd.com/license.html&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;@Rill/http to the Rescue&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://medium.com/@pierceydylan/"&gt;The creators of rill are&lt;/a&gt; pretty smart. When they created rill to work in the browser and in node, they realized the same thing I did at the beginning of this article — that dissimilar APIs are what make isomorphic code so difficult. So, to ensure that it works in both places, they created their own implementation of node’s low level &lt;a href="https://nodejs.org/api/http.html"&gt;http module&lt;/a&gt;, &lt;a href="https://github.com/rill-js/http"&gt;@rill/http&lt;/a&gt;, that works in the browser. Because it follows the same API, it can be, with a few tweaks, swapped into other modules that depend upon http, and allow them to work in the browser.&lt;/p&gt;

&lt;p&gt;And that’s exactly what I’ve done with &lt;a href="https://github.com/johnhenry/koa-2-browser"&gt;koa-2-browser&lt;/a&gt;. It’s literally koa that works in the browser! It’s a light framework, so it integrates easily with other code. And because the changes are relatively minor, I’m working on landing these changes in an upcoming release of Koa as to make it an isomorphic application out of the box. For now, you can take [nearly] all code that you wrote for a koa server in node, and transfer it to the browser.&lt;/p&gt;

&lt;p&gt;Revisiting the service worker example above, we have:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//example -- koa-2-browser
const server = require(‘koa-2-browser’)();
server.use(/*middleware*/);
server.listen(()=&amp;gt;{

self.onfetch(function(event) {
    return event.respondWith(new Promise((resolve, reject)=&amp;gt;{
      const response = server.respondTo(event.request,{browserResponse:true});
      response.on(“finish”, (res)=&amp;gt;{
        resolve(res);
      });
    }));
  });

});

//Note:server.RespondTo returns a response, (here identified as "response") object that is not a suitable resolution for the promise passed to event.respond with. Passing the "browserResponse:true" option will cause the "finish" event to be resolved with a suitable instance of window.Response (identified as "res").
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It’s important to realize that if we ever want to move towards isomorphic JavaScript, we should really shy away from competing standards within the language itself.&lt;/p&gt;

&lt;p&gt;And this is true for open source software as a whole; if two different APIs do the same thing, they should have the same structure. When we set out to create something new, we should pay attention to and work with others who are already doing similar things in other environments. The less time we spend re-inventing the wheel, the more time we have to invent new and exciting stuff (hover-wheel anyone?).&lt;/p&gt;

&lt;h2&gt;
  
  
  Et tu, Buffer?
&lt;/h2&gt;

&lt;p&gt;Servers aren’t the only competing standards across environments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://nodejs.org/api/buffer.html"&gt;**Buffers&lt;/a&gt;** are standardized in node and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"&gt;**ArrayBuffers&lt;/a&gt;** are standardized in the browser. They are both containers for binary data, but they work slightly differently. This is particularly annoying when attempting to create isomorphic applications. Even worse, some objects that have a “buffer” method, may return an ArrayBuffer instead, meaning that you may have to apply special logic to work with them in different environments.&lt;/p&gt;

&lt;p&gt;After many revisions, &lt;a href="https://nodejs.org/api/stream.html"&gt;**Streams&lt;/a&gt;** are close to full standardization in node. Unfortunately, &lt;a href="https://streams.spec.whatwg.org/"&gt;**Browser Streams&lt;/a&gt;**, an emerging standard, introduce new and incompatible parts of the API. Hopefully, the groups involved can come to an agreement at some point.&lt;/p&gt;

&lt;p&gt;There are some other similar topics like &lt;strong&gt;importing modules&lt;/strong&gt;, but that situation is way more complicated to go into her.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus Library!!!
&lt;/h2&gt;

&lt;p&gt;But trying new things is fun! I actually prefer the &lt;a href="https://github.com/flyweb/spec"&gt;way that FlyWeb creates servers&lt;/a&gt;. Since it &lt;a href="https://github.com/koajs/koa/issues/482"&gt;it appears that I’m not the only one&lt;/a&gt;, I’ve created another library, &lt;a href="https://github.com/johnhenry/flyweb-koa"&gt;flyweb-koa&lt;/a&gt;. It allows you to use koa in a manner similar to the FlyWeb, while maintaining everything that koa has to offer.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const koa = require(“koa-2-browser”);
//This also works with koa;

const publishServer = require(‘flyweb-koa’)(‘koa’);

const server = await publishServer(/*listening address*/);
server.use(/*middleware*/);
server.onfetch = (context)=&amp;gt;{
...
};
server.respondTo(/*request*/);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>browsers</category>
      <category>servers</category>
      <category>api</category>
    </item>
  </channel>
</rss>
