<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>ReactJs NodeJs Azure</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/reactjsnodejsazure/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/reactjsnodejsazure/feed.xml</link>
    <description>Just another Developer Network site</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:50:24 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>How to Create End-to-End ReactJs NodeJs Express Web Application using SQLdb having Azure AD Authentication using Git for Continuous Deployment</title>
      <link>https://docs.microsoft.com/archive/blogs/reactjsnodejsazure/reactjs-nodejs-express-azure-web-app</link>
      <pubDate>Tue, 01 Aug 2017 14:44:19 GMT</pubDate>
      <dc:creator><![CDATA[Raveena Dayani]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/reactjsnodejsazure/?p=1</guid>
      <description><![CDATA[This blog is to ensure that if you have not worked before with the SERN technology stack with Azure...]]></description>
      <content:encoded><![CDATA[This blog is to ensure that if you have not worked before with the SERN technology stack with Azure Active Directory Authentication on Continuous Deployment using Git, it will guide you to be able to deploy one end-to-end Azure web application with SQL as the database, ReactJs doing the client-side rendering as our front-end, APIs coded in NodeJs and Express Web Framework wiring up the APIs and our entire middleware with the SQL database.
<h3>Create React App</h3>
Assuming you have node already installed on your system, to start creating a reactjs application for client-side rendering, go to command prompt and type the following command:
<blockquote>
<pre><span style="color: #008000">npm install -g create-react-app</span> [g is for global install of the npm package]</pre>
<pre><span style="color: #008000"><a target="_blank" href="https://github.com/facebookincubator/create-react-app" rel="noopener noreferrer">create-react-app</a> newreactjsclientproject</span></pre>
</blockquote>
Node is required in the system only to use npm to install all the packages &amp; their dependencies (you'll find them in a folder by the name of node_modules). You may now start writing code in <em>newreactjs</em><em>clientproject. </em>
<blockquote>
<pre><span style="color: #008000"><code>my-app
├── README.md
├── node_modules </code></span><span style="color: #000000"><code>[folder: containing all the npm package dependencies]</code></span><span style="color: #008000"><code>
├── package.json <span style="color: #000000">[all the package dependencies go here*]</span>
├── .gitignore
├── public <span style="color: #000000">[folder: will be containing the production build of your final application]</span>
│   └── favicon.ico <span style="color: #000000">[just the icon image in the browser tab - not important unless you plan to go live ]</span>
│   └── index.html <span style="color: #000000">[main static page - very important but no editing really required (except for <em>title</em> tag)]</span>
│   └── manifest.json<span style="color: #000000"> [metadata about your application - not important unless you plan to go live]</span>
└── src <span style="color: #000000">[folder: all the source code for your application goes here - ever hot and happening folder]</span>
    └── App.css <span style="color: #000000">[all the css for your application]</span>
    └── App.js <span style="color: #000000">[the startup page of your application]</span>
    └── App.test.js
    └── index.css<span style="color: #000000"> [not important - unless you want to set font family etc]</span>
    └── index.js <span style="color: #000000">[actual startup page of your app; will inform the app that App.js is the startup page**]</span>
    └── logo.svg
    └── registerServiceWorker.js <span style="color: #000000">[not to be touched unless you want a slow running app - in production, a service worker gets registered to serve the assets from local cache***]</span>

</code></span><code>*The react start script, react build script is also mentioned here in </code><code><em>scripts </em>json<i>; </i>node and npm versions can be mentioned here in <em>engines</em> json; the <em>proxy</em> is also to be mentioned here <strong>(IMP)</strong></code><code> - it is the url to the location where the server will be found running
</code><span style="color: #008000"><code>
<span style="color: #003300">**This will also make a note of the container where your application will be held. This container will be denoted by the <em>id</em> of the <em>div</em> tag within <em>body</em> of your <em>index.html
</em>
***This lets the app load faster on subsequent visit to live site, gives your site offline capabilities - It is a new concept of <a target="_blank" href="https://goo.gl/KwvDNy" rel="noopener noreferrer">Progressive Web Apps</a>. Moving forward this is going to eliminate the need of app store and will ensure that your web app is reliably fast even on a network as slow as 2G.</span></code></span></pre>
</blockquote>
After generating your react project,  to run the react application on a localhost port, use the below command:
<blockquote>
<pre><span style="color: #339966">npm start </span></pre>
</blockquote>
This will start your application on https://localhost:3000. Port number can vary and depends on the availability of the port.

Note: The best thing about reactjs is that we can edit at runtime and see the changes getting reloaded on the running localhost by itself, i.e., without refreshing the page or hitting F5.

After your app has started, just notice the <em>static</em> folder by hitting key F12 to get the developer tools in your browser.
<blockquote><img src="https://msdnshared.blob.core.windows.net/media/2017/08/12-265x300.png" alt="" width="265" height="300" class="alignnone size-medium wp-image-35" /></blockquote>
These <em>static</em> folder contents have appeared here as <em>production build</em> assets of your react application. When making an e2e application with NodeJs, you'll need to create a production build beforehand, by yourself. For that, you can simply do this command  in your command prompt:
<blockquote>
<pre><span style="color: #339966">npm run build</span></pre>
</blockquote>
When you run this command, the <em>production build static assets </em> will land up in the <em>build</em> folder.

<span>You just need to copy the contents of this <em>build</em> folder from our React application to the <em>public</em> folder of our Express Node application (which we'll create below). </span>When we come to <a href="https://blogs.msdn.microsoft.com/reactjsnodejsazure/2017/08/01/reactjs-nodejs-express-azure-web-app/">server side application using Express</a>, we'll visit this point again and discuss more for further clarity.

When you create a react application using the Create React App, as of today, it installs:
[we don't need to do any development with any of the following. create-react-app takes care of that for us. It is just for informational purpose.]
<ol>
 	<li><em>webpack </em>- correctly bundles React in production mode and optimizes the build for the best performance. The build is minified and the filenames include the hashes. By default, it also includes a service worker so that your app loads from the local cache on future visits.</li>
 	<li><em>babel</em> -  transpiles JSX (react language) script written in ES6 <a href="https://en.wikipedia.org/wiki/ECMAScript">(ECMAScript 6)</a> into javascript</li>
 	<li><em>eslint </em>- identifies and reports patterns found in javascript code (as far as my needs, it seemed like resharper which reports unused variables and methods and packages)</li>
 	<li>etc..</li>
</ol>
<h3>Material-UI With ReactJs</h3>
Before modifying the App.js to be able to call server API, install material-ui npm package:
<blockquote>
<pre><span style="color: #339966">npm install --save material-ui
</span></pre>
</blockquote>
To be able to listen onTouchTap event on any material-ui component, edit your <em>index.js</em> to add the below 2 statements exactly after imports:
<div>
<blockquote>
<pre><span style="color: #339966">import injectTapEventPlugin from 'react-tap-event-plugin';</span>
<span style="color: #339966">injectTapEventPlugin</span><span><span style="color: #339966">()</span>;</span></pre>
</blockquote>
</div>
Modify the App.js file to look like following:
<div>
<blockquote>
<pre><span style="color: #339966">import React, { Component } from 'react';</span>
<span style="color: #339966">import logo from './logo.svg';</span>
<span style="color: #339966">import './App.css';</span>
<span style="color: #339966">import RaisedButton from 'material-ui/RaisedButton';</span>
<span style="color: #339966">import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';</span>
<span style="color: #339966">import getMuiTheme from 'material-ui/styles/getMuiTheme';</span>
<span style="color: #339966">import { cyan700 } from 'material-ui/styles/colors';</span>
<span style="color: #339966">const muiTheme = getMuiTheme({</span>
<span style="color: #339966"> palette: {</span>
<span style="color: #339966"> textColor: cyan700,</span>
<span style="color: #339966"> },</span>
<span style="color: #339966"> appBar: {</span>
<span style="color: #339966"> height: 50,</span>
<span style="color: #339966"> },</span>
<span style="color: #339966">});</span>

<span style="color: #339966">class App extends Component {</span>
<span style="color: #339966"> constructor(props) {</span>
<span style="color: #339966"> super(props);</span>
<span style="color: #339966"> this.state = { users: [] }
</span><span style="color: #339966">}</span>

<span style="color: #339966"> handleOpen() {</span>
<span style="color: #339966">  fetch('/api/users', {</span>
<span style="color: #339966">   method: 'GET',</span>
<span style="color: #339966">   headers: {</span>
<span style="color: #339966">    'Content-Type': 'application/json',</span>
<span style="color: #339966">    'Accept': 'application/json',</span>
<span style="color: #339966">   }</span>
<span style="color: #339966">  })</span><span style="color: #339966">.then(res =&gt; res.json())</span>
<span style="color: #339966">    .then(users =&gt; this.setState({ users }));</span>
<span style="color: #339966"> }</span>

<span style="color: #339966"> render() {</span>
<span style="color: #339966">  return (</span>
<span style="color: #339966">  &lt;div className="App"&gt;</span>
<span style="color: #339966">    &lt;div className="App-header"&gt;</span>
<span style="color: #339966">      &lt;img src={logo} className="App-logo" alt="logo" /&gt;</span>
<span style="color: #339966">      &lt;h2&gt;Welcome to React&lt;/h2&gt;</span>
<span style="color: #339966">    &lt;/div&gt;</span>
<span style="color: #008000"><strong>    &lt;MuiThemeProvider muiTheme={muiTheme}&gt;
      &lt;RaisedButton label="Call API" onTouchTap={this.handleOpen.bind(this)} /&gt;
    &lt;/MuiThemeProvider&gt;
    &lt;br /&gt;&lt;br /&gt;
    {this.state.users.length !== 0 ? "api called! check response of call to users in Network Tab in F12" : "api not yet called"}</strong></span>
<span style="color: #339966">  &lt;/div&gt;</span>
<span style="color: #339966">  );</span>
<span style="color: #339966"> }</span>
<span style="color: #339966">}</span>

<span style="color: #339966">export default App;</span></pre>
</blockquote>
</div>
<h3>Create an Express App</h3>
to start creating an express application for server-side work, go to command prompt and type the following command:
<blockquote>
<pre class="language-sh"><span style="color: #008000"><code class="language-sh">npm install express-generator -g
</code></span><span style="color: #008000">express newnodeserverexpressproject</span></pre>
</blockquote>
<span>This third command will quickly create an application skeleton. You may now start writing your APIs and other middleware code in </span><em>newnodeserverexpressproject</em><em>. </em>

&nbsp;
<blockquote>
<pre class="language-sh"><code class="language-sh">
<span style="color: #339966">├── app.js  <span style="color: #000000">[this is the <em>main file</em> of the server - all the apis and api routes and middleware code goes here]</span>
├── bin 
│   └── www  <span style="color: #000000">[this is the startup file. It tells the port the server will listen on and the <em>main file </em>name]</span>
├── package.json </span>[all the package dependencies go here*]<span style="color: #339966">
├── public  </span>[folder: all static files from the react app are to be copied here from react app's build folder]<span style="color: #339966">
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes </span>[below 2 are default api routes set in the express app generated temeplate]<span style="color: #339966">
│   ├── index.js 
│   └── users.js
└── views </span>[in case server side rendering is to be done, then only these views are needed**]<span style="color: #339966">
    ├── error.pug
    ├── index.pug
    └── layout.pug
</span>
*The node start script, node build script go in <em>scripts</em> json

**If server-side rendering not needed, views can be ignored, and you'll see plain text output while calling apis]

</code></pre>
</blockquote>
Before starting express node application, you first need to install npm package dependencies locally into this express app. It will create a node_modules folder for express app as well.
<blockquote>
<pre><span style="color: #339966">npm install <span style="color: #000000">[Note that node_modules folder in express/react app can be removed at any time and recreated using npm install - it identifies all the dependencies by itself]</span></span></pre>
</blockquote>
To start running the application, use below command. And, the express app on localhost would start like the following screenshot shows:
<blockquote>
<pre><span style="color: #339966">node ./bin/www

<img src="https://msdnshared.blob.core.windows.net/media/2017/08/22.png" alt="" width="188" height="192" class="alignnone wp-image-45" />
</span></pre>
</blockquote>
Now your server is running on localhost on some available port (you can notice it in your browser).

If you want to see the api run on the server, add /api/users to the url:
<blockquote><img src="https://msdnshared.blob.core.windows.net/media/2017/08/111.png" alt="" width="304" height="82" class="alignnone wp-image-127" /></blockquote>
&nbsp;
<ul>
 	<li><strong>For development on localhost</strong> with both api server and react's webpack server, the react client should know the server location, hence, one tiny change would be needed in the react app's package.json</li>
</ul>
<div>
<blockquote>
<pre><span style="color: #339966">"proxy": "https://localhost:3000/"  <span style="color: #000000">[assuming 3000 is the port on which server is listening]</span></span><span> </span></pre>
</blockquote>
</div>
<p style="padding-left: 30px">This proxy would tell the react app to look for the API calls at the aforementioned URL.</p>
<p style="padding-left: 30px">Note: proxy in package.json of react app would be used only for local development, i.e., when the API server is still in development and not yet published. (<a target="_blank" href="https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/" rel="noopener noreferrer">Refer</a> - below image taken from here)</p>

<blockquote><img src="https://msdnshared.blob.core.windows.net/media/2017/08/9.png" alt="" width="608" height="128" class="alignnone wp-image-97" /></blockquote>
<p style="padding-left: 30px">To keep the server running and to reflect the changes made in the code on the running server without havint to restart the server, install nodemon package:</p>

<blockquote>
<pre><span style="color: #339966">npm install --save nodemon</span></pre>
</blockquote>
<p style="padding-left: 30px">After this, instead of calling <em>node ./bin/www</em>, call the following command:</p>

<blockquote>
<pre><span style="color: #339966">nodemon ./bin/www</span></pre>
</blockquote>
<p style="padding-left: 30px">Let the express node server running. Run the react client from a separate command window by doing an npm start. It will give a prompt that port 3000 is already being used (yes! because we are using it for node server as shown above). Agree to start react webpack dev server on a different port. It fires up the react app which looks like this:</p>

<blockquote><img src="https://msdnshared.blob.core.windows.net/media/2017/08/121.png" alt="" width="302" height="228" class="alignnone wp-image-135" /></blockquote>
<p style="padding-left: 30px">When we click on the "Call API" button, the output from browser as well as from the network tools looks like this:</p>

<blockquote><img src="https://msdnshared.blob.core.windows.net/media/2017/08/13.png" alt="" width="768" height="224" class="alignnone wp-image-145" /></blockquote>
<p style="padding-left: 30px">This proves that the react app webpack server running at port 3001 makes the proxy call to api (express node) server which was already running on port 3000.</p>

<ul>
 	<li><strong>For a published app</strong>, we've to copy the production build contents of react app from above react directory into the public folder of express app. Check in those changes for continuous deployment (below). The express app will start referring to the static files in its public folder and proxy would be ignored there.</li>
</ul>
<h3>Continuous Deployment using Github on Azure Web App</h3>
Add a <em>.gitignore</em> file to the express directory in your system. Its contents should be like this for your express node app:
<blockquote>
<pre><span style="color: #339966"># See https://help.github.com/ignore-files/ for more about ignoring files.</span>

<span style="color: #339966"># dependencies</span>
<span style="color: #339966">/node_modules</span>

<span style="color: #339966"># testing</span>
<span style="color: #339966">/coverage</span>

<span style="color: #339966"># production</span>
<span style="color: #339966">/build</span>

<span style="color: #339966"># misc</span>
<span style="color: #339966">.DS_Store</span>
<span style="color: #339966">.env.local</span>
<span style="color: #339966">.env.development.local</span>
<span style="color: #339966">.env.test.local</span>
<span style="color: #339966">.env.production.local</span>

<span style="color: #339966">npm-debug.log*</span>
<span style="color: #339966">yarn-debug.log*</span>
<span style="color: #339966">yarn-error.log*</span></pre>
</blockquote>
This will tell node to ignore node_modules while checking in the code. Create a repository in github. Follow the steps as given in github on creating a new repo, as below:
<blockquote>
<pre class="f5 js-zeroclipboard-target"><span style="color: #339966"><span class="user-select-contain">echo "# z" &gt;&gt; README.md</span>
<span class="user-select-contain">git init</span>
<span class="user-select-contain">git add README.md</span>
<span class="user-select-contain">git commit -m "first commit"</span>
<span class="user-select-contain">git remote add origin <span class="js-git-clone-help-text">https://github.com/myaccount/repo.git</span></span>
<span class="user-select-contain mb-0">git push -u origin master</span></span></pre>
</blockquote>
At this point, you have added a readme file to your repo, concluding that your repo is up and running. Now add all your express files along with the production build of react which you copied to its public folder:
<blockquote>
<pre><span style="color: #339966">git add . <span style="color: #000000">[This will stage all the changes in your local]</span></span>
<span style="color: #339966">git commit -m "express node server app and prod build of react client"</span>
<span style="color: #339966">git push -u origin master</span></pre>
</blockquote>
Now, go to you Azure Web App. From the Menu options in the left, select <em>Deployment Options</em>.
<blockquote><img src="https://msdnshared.blob.core.windows.net/media/2017/08/72.png" alt="" width="239" height="430" class="alignnone size-full wp-image-125" /></blockquote>
If no github account is configured already, it will ask you to configure one:
<blockquote><img src="https://msdnshared.blob.core.windows.net/media/2017/08/14.png" alt="" width="397" height="273" class="alignnone wp-image-155" /></blockquote>
Select source and your repository in that source which you want to configure for continuous deployment. And hit Ok. You'll see your github commits syncing here. If not, hit <em>Sync</em> to start syncing.
<blockquote><img src="https://msdnshared.blob.core.windows.net/media/2017/08/81.png" alt="" width="390" height="172" class="alignnone size-full wp-image-95" /></blockquote>
You have successfully configured your continuous deployment. This point onwards, whatever commits land up in github will be synced to your web app as well and update it simulataneously.

After your app has started, just <span>notice that a </span><em>static</em><span> folder is there, by hitting key F12 to get the developer tools in your browser.</span>
<blockquote><img src="https://msdnshared.blob.core.windows.net/media/2017/08/15.png" alt="" width="184" height="163" class="alignnone size-full wp-image-165" /></blockquote>
If you switch to any api from the url of the published webapp, say,
<blockquote>
<pre><span style="color: #339966">https://blogreactnode.azurewebsites.net/api/users</span></pre>
</blockquote>
Then the source contents (as can be observed from f12 dev tools) change by itself in published app.
<blockquote><img src="https://msdnshared.blob.core.windows.net/media/2017/08/10.png" alt="" width="239" height="131" class="alignnone size-full wp-image-115" /></blockquote>
Hence, we don't need <em>proxy</em> here like we did during local development.

<span style="text-decoration: underline">Addendum as on 20th </span>Sept,<span style="text-decoration: underline"> 2017:</span>
<h3>How to Check Logs in Azure App When Website Doesn't Work as Expected</h3>
In Azure App, Go to Log Stream under Monitoring button, in the Menu on left.

In case the logs are turned off by default, go to Diagnostic Logs right above Log Stream and turn on the necessary logs and select appropriate logging level. Log Stream would look like below when ready:

<img src="https://msdnshared.blob.core.windows.net/media/2017/08/Log-Stream-Logs-300x68.png" alt="" width="300" height="68" class="alignnone size-medium wp-image-176" />
<h3>When the Logs give 500 TypeError</h3>
Lately, it happened for me that even a new Express App didn't get deployed correctly. From Stack Overflow, got the hint to check the node version on the App Service and whether the one on your local machine is even supported on Azure App Service.

To check the version on the App Service with the help of this <a href="https://blogs.msdn.microsoft.com/azureossds/2016/04/20/nodejs-and-npm-versions-on-azure-app-services/">MSDN blog</a>:
<ul>
 	<li style="list-style-type: none">
<ul>
 	<li>Go to Kudu from the left menu</li>
 	<li>In the Command Prompt window below in Debug Console, go to D:<span>\Program Files (x86)\nodejs to check the list of available versions. It will give a hint as to whether Azure Service supports the node version you had worked on in your local machine.</span></li>
 	<li>To let the Azure Infra know about which version you'd support, put the below code into the <em>package.json</em> of the Express Application.</li>
</ul>
</li>
</ul>
<blockquote>
<pre><span style="color: #339966">"engines": {
  "node" : "5.9.1",
  "npm" : "3.7.3"
}</span></pre>
&nbsp;</blockquote>
<h6><strong><em>&lt;&lt;&lt;This post is currently incomplete. AAD AuthN, Node APIs AuthZ, React pages AuthZ and SQL DB integration are pending...&gt;&gt;&gt;ss</em></strong></h6>]]></content:encoded>
    </item>
  </channel>
</rss>