<?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: Chance Murray</title>
    <description>The latest articles on DEV Community by Chance Murray (@chancesm).</description>
    <link>https://dev.to/chancesm</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%2F92899%2Ffb1509e4-0bfd-41f9-bf44-1cc2dfe444fd.png</url>
      <title>DEV Community: Chance Murray</title>
      <link>https://dev.to/chancesm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chancesm"/>
    <language>en</language>
    <item>
      <title>Running NodeJS Forever!</title>
      <dc:creator>Chance Murray</dc:creator>
      <pubDate>Fri, 09 Mar 2018 05:02:09 +0000</pubDate>
      <link>https://dev.to/chancesm/running-nodejs-forever-3eof</link>
      <guid>https://dev.to/chancesm/running-nodejs-forever-3eof</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;NodeJS is a great tool for web development, but it has it’s hang-ups. When setting up an Express Server with NodeJS, many restarts are required and expected during the development stage. Not only that, but it would be great to keep a NodeJS project running indefinitely once you close your terminal session. This tutorial aims to address both of those issues. This tutorial will start by addressing the desire to keep a nodeJS process running and will then address reloading the process when changes are made. I will do this by addressing different tools used to accomplish these tasks and will end with my preferred option. Let’s begin.&lt;/p&gt;

&lt;p&gt;NOTE: THIS IS COPIED FROM A WORDPRESS BLOG AND THEREFORE SOME STYLES AND TEXT DID NOT TRANSFER OVER PROPERLY&lt;/p&gt;




&lt;h1&gt;
  
  
  Video (for all of you non-readers out there)
&lt;/h1&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/7fpSFwwNqYA"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h1&gt;
  
  
  The Options:
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Tmux/Screen (Okay Option)
&lt;/h2&gt;

&lt;p&gt;Tmux and Screen are terminal multiplexers that allow you to create “sessions” that can persist after you close your terminal. They create their own sockets that a terminal instance runs in. A user can then access this through their tool’s commands. Once inside one of these sessions, running a command with node and then leaving the session will not end the node process because the terminal session is still open. I won’t go into detail here, but if you watch the video, I will show how to do this with tmux.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nodemon (Better Option)
&lt;/h2&gt;

&lt;p&gt;One of the issues with the above method is that you still need  to stop and restart the node process every time you make a change to the server. One work-around for this is to install nodemon, a nodejs wrapper that watches the files in the directory it is run it and restarts. Nodemon is a node module and can be installed with npm. Combining nodemon with tmux or screen will allow you to run a node server that restarts whenever a change occurs. There is an easier way.&lt;/p&gt;

&lt;h2&gt;
  
  
  PM2 (Best Option)
&lt;/h2&gt;

&lt;p&gt;PM2 is a Process Manager for NodeJS. Like nodemon, it is also installed globally through npm. Unlike nodemon, pm2 actually creates a system process independent of any terminal instance to run your node process in. The video shows how to set it up after installing so I won’t discuss it here. &lt;a href="http://pm2.keymetrics.io/docs/usage/quick-start/"&gt;They have some great documentation at their site.&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;There are different ways to accomplish the task of running node forever, but my favorite, since I first started using it, is PM2. It allows users to monitor multiple node processes, watch the files for changes, and even run in a clustered mode for load-balancing purposes.&lt;/p&gt;

</description>
      <category>tutorials</category>
      <category>express</category>
      <category>node</category>
    </item>
    <item>
      <title>Setting up a basic ExpressJS App with NodeJS</title>
      <dc:creator>Chance Murray</dc:creator>
      <pubDate>Tue, 13 Feb 2018 22:50:08 +0000</pubDate>
      <link>https://dev.to/chancesm/setting-up-a-basic-expressjs-app-with-nodejs-4bjb</link>
      <guid>https://dev.to/chancesm/setting-up-a-basic-expressjs-app-with-nodejs-4bjb</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you want to get started with NodeJS web apps, here’s a great place to start. We will build a basic app that displays Quotes on a page. This tutorial covers the following steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setting up a NodeJS project&lt;/li&gt;
&lt;li&gt;Creating a web server with express&lt;/li&gt;
&lt;li&gt;Using the EJS template engine library&lt;/li&gt;
&lt;li&gt;Sending external data to our templates with Express Middleware&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;NOTE: THIS IS COPIED FROM A WORDPRESS BLOG AND THEREFORE SOME STYLES AND TEXT DID NOT TRANSFER OVER PROPERLY&lt;/p&gt;




&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;For this tutorial, I will assume you have NodeJS and NPM installed on your development environment. If you don’t have either of those things installed, I recommend the following links depending on your operating system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Windows

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://lmgtfy.com/?q=Install+NodeJS+and+NPM+on+Windows"&gt;http://lmgtfy.com/?q=Install+NodeJS+and+NPM+on+Windows&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Linux (Ubuntu) and Mac OS X

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://nodesource.com/blog/installing-node-js-tutorial-using-nvm-on-mac-os-x-and-ubuntu/"&gt;http://nodesource.com/blog/installing-node-js-tutorial-using-nvm-on-mac-os-x-and-ubuntu/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this tutorial I will be using Node version 8.9.4 and NPM version 5.6.0.&lt;/p&gt;

&lt;p&gt;If you have Node and NPM installed, running the following commands in your terminal will check the versions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NODE VERSION:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;node -v&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;NPM VERSION:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npm -v&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lastly, you should have a good code editor ready. I use Visual Studio Code. It is a great editor, with a built-in file manager and an integrated terminal. You can download it from &lt;a href="https://code.visualstudio.com"&gt;this site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now that you are set up, it is time to start coding!&lt;/p&gt;




&lt;h2&gt;
  
  
  Video (for all of you non-readers out there)
&lt;/h2&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/kIAxZq1rew0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Walk-through
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Creating A Node Project
&lt;/h3&gt;

&lt;p&gt;Start by creating a directory you would like to work out of. Mine is ~/myapp. Using your terminal, move into this directory and run the following command:&lt;code&gt;npm init -y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This should have created a package.json file in your directory. Create a file that matches the “main” key in you package.json file. My file is index.js. Now that we have the project ready, we can move on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Dependencies
&lt;/h3&gt;

&lt;p&gt;For the app we will be using Express as our web server so we need to install the Express module using npm. From your project directory open a terminal and run the command &lt;code&gt;npm i -s express ejs&lt;/code&gt;. You should now have a node_modules directory. Express and the EJS template engine were installed in that directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating your Express App
&lt;/h3&gt;

&lt;p&gt;You need to pull express from the node_modules folder and use it in your index.js file. to do this, add the following lines to the top of index.js:&lt;br&gt;&lt;br&gt;
&lt;code&gt;var express = require('express')&lt;br&gt;
var app = express()&lt;br&gt;
&lt;/code&gt;&lt;br&gt;&lt;br&gt;
This creates an app using express that we will add functionality to in the next steps.&lt;/p&gt;

&lt;p&gt;In order to run the express app, you need to add the following line to the bottom of your index.js file:&lt;br&gt;&lt;br&gt;
&lt;code&gt;app.listen(3000)&lt;/code&gt;&lt;br&gt;&lt;br&gt;
This runs your server at “&lt;a href="http://localhost:3000%E2%80%9D"&gt;http://localhost:3000”&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
Keep this line at the bottom of your index.js file. Whenever we add more code to the page, we will add it above this line.&lt;/p&gt;
&lt;h3&gt;
  
  
  Serving Static Files
&lt;/h3&gt;

&lt;p&gt;We need a folder that we can serve our static files (css, images, javascript, etc.) from.&lt;/p&gt;

&lt;p&gt;Create a folder called ‘public’ in your directory. This will hold our static files.&lt;/p&gt;

&lt;p&gt;Add the following line to your index.js file:&lt;br&gt;&lt;br&gt;
&lt;code&gt;app.use(express.static('public'))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Whe a static resource is requested, your app will now look in the public folder. as a note, the app does nnot consider the name of the public foldder in the request. For example, f the page looks for ‘js/test.js’, you app will look for it in ‘public/js/test/js’.&lt;/p&gt;
&lt;h3&gt;
  
  
  Adding Routes
&lt;/h3&gt;

&lt;p&gt;Our first route will handle a simple GET request to our base route ‘/’. This means that any GET request sent specifically to “&lt;a href="http://localhost:3000%E2%80%9D"&gt;http://localhost:3000”&lt;/a&gt; will be handled by this route. This is where we will eventually have the logic for our main page.&lt;/p&gt;

&lt;p&gt;To get started, add the following lines to your index.js file:&lt;br&gt;&lt;br&gt;
&lt;code&gt;app.get('/', function (req, res) {&lt;br&gt;
res.send('HELLO WORLD')&lt;br&gt;
})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can test to see if this works by opening a terminal in your directory and running &lt;code&gt;node index.js&lt;/code&gt;. Once you have done that, you can navigate to ‘&lt;a href="http://localhost:3000%E2%80%99in"&gt;http://localhost:3000’in&lt;/a&gt; your browser and you should see your content.&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating a View Template with EJS
&lt;/h3&gt;

&lt;p&gt;This takes a few steps. First, create a ‘views’ folder in your app directory. Next, create a file in your ‘views’ directory called ‘index.ejs’. Put the following content in that file:&lt;br&gt;&lt;br&gt;
`&amp;lt;!DOCTYPE html&amp;gt;&lt;br&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Quotes


.quote{
border: 2px solid black;
margin: 5px;
}




&lt;h1&gt;Quotes:&lt;/h1&gt;



&lt;p&gt;~ &lt;/p&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;Next, you have to tell the app to render this file when the ‘/’ route is requested.&lt;/p&gt;

&lt;p&gt;To do this, you first need to add the following two lines to your ‘index.js’ file before your route, and after you declare ‘app’:&lt;br&gt;&lt;br&gt;
&lt;code&gt;app.set('views', 'views');&lt;br&gt;
app.set('view engine', 'ejs');&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Last, you need to tell the app to render the file by replacing  &lt;code&gt;res.send('HELLO WORLD')&lt;/code&gt; in your route with the following line:&lt;br&gt;&lt;br&gt;
&lt;code&gt;res.render('index')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you run your server, it should show you your template file with an example quote.&lt;/p&gt;

&lt;p&gt;We are ready for the final step.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sending Data to the View
&lt;/h3&gt;

&lt;p&gt;To accomplish this step, we are going to need to do some setup. First, we will need to create a folder called ‘controllers’ that will contain all of our data-handling files and functions. Net, we will create a file called ‘quotes.js’ in our controllers folder. In that file, we will add some dummy data:&lt;br&gt;&lt;br&gt;
&lt;code&gt;var data = [&lt;br&gt;
{&lt;br&gt;
author: "Chance",&lt;br&gt;
quote: "Cliche web tutorials are lame."&lt;br&gt;
},&lt;br&gt;
{&lt;br&gt;
author: "Yoda",&lt;br&gt;
quote: "Do, or do not. There is no 'try'"&lt;br&gt;
},&lt;br&gt;
{&lt;br&gt;
author: "Ben Franklin",&lt;br&gt;
quote: "A penny saved is a penny earned."&lt;br&gt;
},&lt;br&gt;
{&lt;br&gt;
author: "Dr. Seuss",&lt;br&gt;
quote: "Don't cry because it's over, smile becuse it happened."&lt;br&gt;
}&lt;br&gt;
]&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Next, we will export a function that our app can use to get this data:&lt;br&gt;&lt;br&gt;
&lt;code&gt;exports.getData = function() {&lt;br&gt;
return {&lt;br&gt;
quotes: data&lt;br&gt;
}&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;&lt;br&gt;
In ‘index.js’, we need to require the controller. to do this, add the line &lt;code&gt;var Quotes = require('./controllers/quotes');&lt;/code&gt; before your route.&lt;br&gt;&lt;br&gt;
Now that we have the data, we need to send it to our view. Replace &lt;code&gt;res.render('index')&lt;/code&gt; in our route with:&lt;br&gt;&lt;br&gt;
&lt;code&gt;res.render('index', Quotes.getData())&lt;/code&gt;&lt;br&gt;&lt;br&gt;
The view now has the data. Replacing the example div with the following code will allow the view to loop through the data and print it to the page:&lt;br&gt;&lt;br&gt;
`&amp;lt;% for (quote of quotes) { %&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;%=quote.quote%&amp;gt;&lt;/p&gt;

&lt;p&gt;~ &amp;lt;%=quote.author%&amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt;%}%&amp;gt;`&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We did it! Hopefully this tutorial was able to help you with some of the basics of Express, EJS, and NodeJS.&lt;/p&gt;

&lt;p&gt;Thank you for taking the time to read through my first tutorial!&lt;/p&gt;

</description>
      <category>tutorials</category>
      <category>express</category>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
