<?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 Mike Kerslake on Medium]]></title>
        <description><![CDATA[Stories by Mike Kerslake on Medium]]></description>
        <link>https://medium.com/@theonlymikeever?source=rss-a90a79ce48ba------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ASrTozk_YowKLCXa_BXuuA.jpeg</url>
            <title>Stories by Mike Kerslake on Medium</title>
            <link>https://medium.com/@theonlymikeever?source=rss-a90a79ce48ba------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 04:38:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@theonlymikeever/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[How to Run a NodeJS Web Server on a Raspberry Pi for Development]]></title>
            <link>https://medium.com/cloudboost/how-to-run-a-nodejs-web-server-on-a-raspberry-pi-for-development-3ef9ac0fc02c?source=rss-a90a79ce48ba------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ef9ac0fc02c</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Mike Kerslake]]></dc:creator>
            <pubDate>Tue, 21 Nov 2017 22:12:55 GMT</pubDate>
            <atom:updated>2017-11-27T11:20:03.601Z</atom:updated>
            <content:encoded><![CDATA[<p>More use out of the $35 wonderbox</p><h4><strong>Why I Started This Project</strong></h4><p>I’ve had an obsession with electronics and tinkering ever since I was 11 or so. In fact, I remember the first computer I took apart was a a Power Mac G3, only to find out that tinkering on a carpet can lead to a motherboard and me (by an angry pops) being toast. Nonetheless, here I am 17 years later still spending too much time indoors with electronics — this time with a Raspberry Pi 3 sitting next to my TV as a Kodi and RetroPie gaming box.</p><p>After taking a serious step into learning software development for the web this summer, I knew that I wanted to do something more fun with the little machine. I wanted an environment to test out React applications without having to rely on Heroku (which I love, don’t get me wrong). Since NodeJS is incredibly powerful and Rasbian is incredibly flexible, I knew this project was possible. In fact, there’s a lot of tutorials online that helped me along the way, but I wanted to write my own guide for the world.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*TNR_qKa52Z0ZkUsPhM5kMA@2x.jpeg" /></figure><h4><strong>Set Up</strong></h4><p>I’ll start by saying I estimate this project would take a person ~30minutes plus any stackoverflow hole errors. Whatever it takes, grab yourself a cup a coffee first.</p><p>This guide will go over how to use a Rasberry Pi to build a custom NodeJS server so that you may test and run apps during development, or share them with others without the need of a third party host. You’ll need the following to get started:</p><ul><li>A Rasberry Pi (2nd generation or higher)</li><li>An SD card 8GB or higher (I recommend this 32GB from Samsung)</li><li>An HDMI cable</li><li>USB Keyboard</li><li>Access to your Routers configuration panel</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*geYGiPEbduDDKyul6rRqEg@2x.jpeg" /></figure><p>For this project, we’re going to use a standard install of Rasbian. If this is your first time getting started with a Rasberry Pi, then you’ll want to follow this guide for setting up your SD card with <a href="https://www.macworld.co.uk/how-to/mac/how-to-set-up-raspberry-pi-3-with-mac-3637490/">a clean install of Rasbian OS</a>.</p><h4><strong>Installing NodeJS on a Rasberry Pi</strong></h4><p>Once you’re all set up with your OS, it’s time to install Node. From what I’ve read, Rasbian comes with node out of the box, but we’ll want to make sure we have the most recent build. To install the latest version of Node, make sure you’re online and fire up the terminal:</p><pre>sudo su;<br>wget -O - <a href="https://raw.githubusercontent.com/audstanley/NodeJs-Raspberry-Pi/master/Install-Node.sh">https://raw.githubusercontent.com/audstanley/NodeJs-Raspberry-Pi/master/Install-Node.sh</a> | bash;<br>exit;<br>node -v;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*L1PsheQP2md_rVhHOXFEvA@2x.jpeg" /></figure><p>That’s it! Node is now installed (you can check by typing node -v). You can learn more about this <a href="https://github.com/audstanley/NodeJs-Raspberry-Pi">git here</a>.</p><h4><strong>Postgres on a Rasberry Pi</strong></h4><p>While it’s not required for this initial app, you may eventually need a database for your future apps. My flavor of choice is Postgres. To set up Postgres on your RPi, <a href="https://opensource.com/article/17/10/set-postgres-database-your-raspberry-pi">follow this guide</a>.</p><p>Once installed, type psql to enter Postgres and create a DB as follows:</p><pre>$ psql<br>&gt; create database quickreact;</pre><h4><strong>The React App</strong></h4><p>To start, I’m using an incredibly simple react app that includes express, pg (for Postgres), react, react-dom and of course, babel. You can get these installed and saved with the following command:</p><pre>npm install -s express react react-dom pg webpack babel-core babel-loader babel-preset-es2015 babel-preset-react</pre><p>I’ve also installed morgan for middleware logging, as well as chalk for making pretty colors on console.logs. These are optional!</p><pre>npm install -s morgan chalk</pre><p>Before we can run the server, you’re also going to want to install Nodemon globally. Nodemon will continually run your node app, and automatically restart the server whenever and changes are made and saved to the code.</p><pre>npm install nodemon -g</pre><p>Now you’ll only need a few files to get off the ground. A server.js which will set up the express server, an index.js for our main react application, an index.html for our app to load onto, and a first component we’ll call Main.js. We’ll also set up a standard a webpack-config for Babel with the React preset.</p><p>The goal is to start off with something very simple. You probably already know how to build a react app, and the last thing you want are any headaches from your application. You can checkout the <a href="https://github.com/theonlymikeever/quick-react">quick-react on git to get started</a></p><p>To begin we’ll make server.js look something like this:</p><pre>//Requires<br>const express = require(&#39;express&#39;);<br>const app = express();<br>const path = require(&#39;path&#39;);<br>const chalk = require(&#39;chalk&#39;);<br>const morgan = require(&#39;morgan&#39;);</pre><pre>//Static Routes<br>app.use(&#39;/dist&#39;, express.static(path.join(__dirname, &#39;dist&#39;)));<br>app.use(morgan(&#39;dev&#39;)) // logging</pre><pre>//Main App Route<br>app.get(&#39;/&#39;, (req, res, next) =&gt; res.sendFile(path.join(__dirname, &#39;index.html&#39;)));</pre><pre>const port = 1337;<br>//Run Server<br>app.listen(process.env.PORT || port, () =&gt; console.log(chalk.blue(`Listening intently on port ${port}`)));</pre><p>Then a standard setup for React in src/index.js for our application:</p><pre>import React from &#39;react&#39;;<br>import ReactDOM from &#39;react-dom&#39;;<br>import Main from &#39;./Main&#39;;</pre><pre>ReactDOM.render(&lt;Main /&gt;, document.getElementById(‘root’));</pre><p>A quick first Main component; we’ll serve something a bit classic:</p><pre>import React from &#39;react&#39;;</pre><pre>export default function Main () {<br>  return (&lt;h1&gt;Hello World!&lt;/h1&gt;)<br>}</pre><p>Lastly our HTML will need two things, a div tag with an id of “root”, for the app to load into, and then a script tag to request our bundle.js created by web pack:</p><pre>&lt;html&gt;<br>  &lt;head&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;div id=&#39;root&#39;&gt;&lt;/div&gt;<br>    &lt;script src=&#39;/dist/bundle.js&#39;&gt;&lt;/script&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>In our package.json file, I recommend the following scripts, to make building with webpack nice and quick:</p><pre>&quot;scripts&quot;: {<br>    &quot;postinstall&quot;: &quot;npm run webpack&quot;,<br>    &quot;webpack&quot;: &quot;./node_modules/webpack/bin/webpack.js&quot;,<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;,<br>    &quot;start:dev&quot;: &quot;npm run webpack -- -w &amp; nodemon --ignore src/ --ignore dist/ server.js&quot;<br>  }</pre><p>Once all of this is in place, we can npm run start:dev to build and launch our app. Open Chromium on your RPi and navigate to localhost:1337. You should see our beloved message.</p><h4><strong>Running The Server on Boot</strong></h4><p>A working web app on a Raspberry Pi is fun and all, but it’s no use unless we can have the server running all the time. The tricky part is getting your RPi to spin up the server whenever it boots up. In order to do this we’ll need to add a script to the rc.local file in /etc/, which runs everytime your RPi is starting up. First let’s open our file:</p><pre>sudo nano /etc/rc.local</pre><p>On the line before ‘exit 0’ write the following script, replacing ‘NAME_OF_DIRECTORY’ with the directory of your application</p><pre>su pi -c &#39;node /home/pi/NAME_OF_DIRECTORY/server.js &lt; /dev/null &amp;&#39;</pre><p>Write out the lines in order to save them (CTRL-X) and then sudo reboot to restart your RPi.</p><p>Once your machine is up and running again, you should be able to navigate to localhost:1337 and see our ‘Hello World’ without writing a single line in the terminal. See it? Perfect, we’ve hit automation bliss.</p><h4><strong>Port Forwarding</strong></h4><p>This is the last key step. In order for you to serve the application to the outside world, you’re going to need to set up port forwarding through your router. By default most routers will not allow incoming requests to go anywhere on your network.</p><p>Now each provider and router is different, but for the most part you’re going to need access to the port forwarding through admin configuration panel. For this example, I’m using Verizon Fios Myfiosgateway.</p><p>We’re going to need two bits of information: the internal IP address of your RPi and your external IP address for your router. For the former, you can find this by typing sudo ip addr show</p><p>You’ll be given back quite a lot of information but the important line is located under 3: (if you’re using WiFi, 2: if you’re using LAN). See below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*yPOgZxudPVCKULw02doTrA@2x.png" /></figure><p>For your external IP you can find this by typing: ‘What’s my IP address’ into Google.</p><p>Back inside of your port forwarding config panel, you want to set up a rule for the device’s IP (the first address we found). Choose TCP and the port to ‘1337’ (or whatever port you set your node server on in your express app).</p><p>And guess what? That’s it. Now you can navigate to your web app either internally (on the same network) or externally (out and about) — just replace the *IP_ADDRESS* with the external or internal IP:</p><p><a href="http://internal_ip_address:1337/">http://INTERNAL_IP_ADDRESS:1337</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*PYdMD-L8_nZ_TIHQyEFeFQ@2x.png" /></figure><h4><strong>What’s Next?</strong></h4><p>Build an app! Now as much as ‘Hello World’ is the most beautifully overused application on the planet it’s served its purpose here. With this set up, you can essentially create a real-world development server for testing your app in the wild.</p><p>This is new for me too, so I’m going to be experimenting and tweaking the process to make it better. The current caveat here is making sure you build your webpack after you pull down latest versions of your code. In the future I will update this post on how to have your RPi web server automatically do this for you.</p><p><strong>More to come, cheers!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ef9ac0fc02c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cloudboost/how-to-run-a-nodejs-web-server-on-a-raspberry-pi-for-development-3ef9ac0fc02c">How to Run a NodeJS Web Server on a Raspberry Pi for Development</a> was originally published in <a href="https://medium.com/cloudboost">CloudBoost</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>